summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/org/blog/assembly/1.org113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/org/blog/assembly/1.org b/src/org/blog/assembly/1.org
index e268824..3fd21e4 100644
--- a/src/org/blog/assembly/1.org
+++ b/src/org/blog/assembly/1.org
@@ -128,3 +128,116 @@ The values of the registers CS DS and SS are automatically initialized by the OS
 MOV DS, 0x0005 ; Is INVALID
 MOV DS, AX ; This one is VALID
 #+END_SRC
+
+* The ACTUAL thing :
+Enough technical rambling, and now we shall go to the fun part, the ACTUAL CODE. But first, some names you should be familiar with :
+
+- *Mnemonics* : Or *Instructions*, are the...well...Instructions executed by the CPU like *MOV* , *ADD*, *MUL*...etc, they are case *insensitive* but i like them better in UPPERCASE.
+- *Operands* : These are the options passed to the instructions, like *MOV dst, src*, and they can be anything from a memory location, to a variable to an immediate address.
+
+** Structure of an assembly program :
+While there is no "standard" structure, i prefer to go with this one :
+
+#+BEGIN_SRC asm
+    org 100h
+.data
+                                ; variables and constants
+
+.code
+                                ; instructions
+#+END_src
+** MOV dst, src
+The MOV instruction copies the Second operand (src) to the First operand (dst)... The source can be a memory location, an immediate value, a general-purpose register (AX BX CX DX). As for the Destination, it can be a general-purpose register or a memory location.
+
+
+these types of operands are supported:
+#+BEGIN_SRC asm
+MOV REG, memory
+MOV memory, REG
+MOV REG, REG
+MOV memory, immediate
+MOV REG, immediate
+#+END_SRC
+*REG*: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
+
+*memory*: [BX], [BX+SI+7], variable
+
+*immediate*: 5, -24, 3Fh, 10001101b
+
+
+for segment registers only these types of MOV are supported:
+#+BEGIN_SRC asm
+MOV SREG, memory
+MOV memory, SREG
+MOV REG, SREG
+MOV SREG, REG
+SREG: DS, ES, SS, and only as second operand: CS.
+#+END_SRC
+*REG*: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
+
+*memory*: [BX], [BX+SI+7], variable
+
+*** Note : The MOV instruction *cannot* be used to set the value of the CS and IP registers
+** Variables :
+Let's say you want to use a specific value multiple times in your code, do you prefer to call it using something like *var1* or *E4F9:0011* ? If your answer is the second option, you can gladly skip this section, or even better, seek therapy.
+
+Anyways, we have two types of variables, *bytes* and *words(which are two bytes)*, and to define a variable, we use the following syntax
+
+#+BEGIN_SRC asm
+name DB value ; To Define a Byte
+name DW value ; To Define a Word
+#+END_SRC
+
+*name* - can be any letter or digit combination, though it should start with a letter. It's possible to declare unnamed variables by not specifying the name (this variable will have an address but no name).
+*value* - can be any numeric value in any supported numbering system (hexadecimal, binary, or decimal), or "?" symbol for variables that are not initialized.
+
+*** Example code :
+#+BEGIN_SRC asm
+    org 100h
+    .data
+    x db 33
+    y dw 1350h
+
+    .code
+    MOV AL, x
+    MOV BX, y
+#+END_SRC
+
+*** Arrays :
+We can also define Arrays instead of single values using comma separated vaues. like this for example
+#+BEGIN_SRC asm
+    a db 48h, 65h, 6Ch, 6Fh, 00H
+    b db 'Hello', 0
+#+END_SRC
+
+Surprise Surprise, the arrays a and b are identical, the reason behind it is that characters are first converted to their ASCII values then stored in memory!!! Wonderful right ? And guess what, accessing values in assembly IS THE SAME AS IN C !!!
+#+BEGIN_SRC asm
+    MOV AL, a[0] ; Copies 48h to AL
+    MOV BL, b[0] ; Also Copies 48h to BL
+#+END_SRC
+You can also use any of the memory index registers BX, SI, DI, BP, for example:
+#+BEGIN_SRC asm
+MOV SI, 3
+MOV AL, a[SI]
+#+END_SRC
+
+If you need to declare a large array you can use DUP operator.
+The syntax for *DUP*:
+
+number DUP ( value(s) )
+*number* - number of duplicate to make (any constant value).
+*value* - expression that DUP will duplicate.
+
+for example:
+#+BEGIN_SRC asm
+c DB 5 DUP(9)
+;is an alternative way of declaring:
+c DB 9, 9, 9, 9, 9
+#+END_SRC
+one more example:
+#+BEGIN_SRC asm
+d DB 5 DUP(1, 2)
+;is an alternative way of declaring:
+d DB 1, 2, 1, 2, 1, 2, 1, 2, 1, 2
+#+END_SRC
+Of course, you can use DW instead of DB if it's required to keep values larger then 255, or smaller then -128. DW cannot be used to declare strings.