This blog was last modified 429 days before.
Define Variables
To define a variable, we use instructions like the code below:
var_name_here db 12h
ano_var_name dw 5678h
As you can see, db
and dw
could decide the length of the variable.
-
db
Define Byte(s) -
dw
Define Word(s)
Variable could be considered a convenient alias for a specified address.
Where variable stores?
It stores in your code segments, and it's in the same position as where you write it in the asm code. You can use your 8086 emulator to try it out yourself.
For more info, check this Github ASM Learning Book
Some interesting attempt
We already known that variables store in the code segment, means it's possible that the ip
register point to the position of a defined variable, so can we use variable definition to make the CPU run some specified code?
Here we can see the corresponding HEX code of instructions:
mov dx, 0ffffh
ret
is: C3FF FFBA
Then what will happen if we use variable declaration to put these specified numbers into our code segment?
Let compile and run the ASM below:
mov ax, 1
var1 dw 0ffbah
var2 dw 0c3ffh
mov ax, 2
And here is what we got!
DX
value has been updated to ffffh
, and program halted, and this is what command mov dx, 0ffffh
and ret
actually do!
Based on the result we know, the definition of a var before ret
make affect the program, so currently we suggest that all variables be declared after a ret
command, or at a place where IP
register would not go through.
Variables Invalidation
I found out this when testing about DS
data segment register. First we should know that a variable actually is just an alias to a address. For example you now can use a name var_a
to represent a special address like 0b00h
.
However we have already know that the final addresss be accessed not only related to the offset address, but also related to the base address (usually the value in DS
). So if we changed the DS
and then try to directly use the variable, we may got an unexpected result. See screenshot below:
Here we try to read the value from a variable val
after we changed the value of ds
to 0200h
(It's initial value is 0100h
). And we can find out that bx will not turn to 1234h
. Since currently, ds
is 0200h
, so the CPU actually trying to moving the value from address 2000d
to ax
.
You can try to set value back to 0100h
of ds
and see if the program would work again.
Define An Array
byte_arr db 'Hello World', 0
word_arr dw 5 DUP(1234h)
word_arr dw 5 DUP(1, 2, 3)
word_arr dw 1234, 5678, 1234, 0000
There is some points we need to take attentions to:
- When declaring string array, we need to set a stop
00h
at the last of the array. However we won't know where the string array ends. - We could use
dup_times DUP(dup_value)
to create an array with identical value in every element. -
DUP()
could receive more than one parameter. For example you can useDUP(1, 2)
to general array like1, 2, 1, 2, ...
- We could add several contigunous values to an array by using a comma to seperate them.
Access Data In Array
Similar to the one in C/C++
, we could use subscript, or you can say, index, to access the value at the certain postion in the array. See example above.
mov ax, arr[3]
mov arr[5], bx
No comment