Okay, let's pretend that 32bit number is stored at address 0x08048084 and it have value 1234.
Data segment contains data.
Code segment contains code.
From code segment you can access data segment.
Address is a number which represents an offset within a segment.
segment .data
mynumber: dd 1234 ; -> Let's pretend that "mynumber" is at address 0x08048084.
segment .code
;
; Task: How to copy 32 bit value from address 0x08048084 into EAX register.
;
mov eax,[0x08048084] ; -> What value EAX have now? - it's 1234
;
mov eax,[mynumber] ; -> What value EAX have now? - it's 1234
;
mov eax,0x08048084 ; -> What value EAX have now? - it's 0x08048084
;
MOV instruction works like this:
mov destination, source
mov ebx, address ; -> Assign ebx data address.
ebx here stores the 32 bit address of address variable, let's pretend it's 0x08048084
- Here we assign 0x08048084 to EBX. So, EBX now is 0x08048084.
mov eax, [address] ; -> Assign eax a 32 bit value from address, eax now is 1234
eax stores the value at that 0x08048084 address, *0x08048084, which is 1234
- Here a 32 bit value, from address 0x08048084, is stored in EAX register, which is 1234.
inc eax ; -> eax now is 1235
Here's my doubt. inc eax adds 1 to 1234 (and NOT some offset to 0x08048084, did I get it right?).
Yes, eax was 1234, then add 1, and get 1235.
Typing inc eax instead of inc [eax] it's just a synthax simplification?
No.
mov al, [address] ; -> Assign AL a 8 bit value from address, al now is 5
mov ax, [address] ; -> Assign AL a 16 bit value from address, ax now is 6666
Could you explain to me how did you get those two values? They're not related to bytes and words declarations at the beginning of your sample, aren't they?
- Sorry there was my fault, small mistake there, shown and fixed below.
Well, learn though examples:
segment .data
mynumber: dd 1234 ; -> Let's pretend that "mynumber" is at address 0x08048084.
bytearray: db 5,22,17,26
wordarray: dw 4423,7724,3356
segment .code
mov al,[bytearray] ; -> AL now is 5
mov al,[bytearray+1] ; -> AL now is 22
mov al,[bytearray+2] ; -> AL now is 17
mov al,[bytearray+3] ; -> AL now is 26
mov al,[bytearray] ; -> AL now is 5
add al,[bytearray+1] ; -> AL now is 5+22 = 27
inc byte [bytearray]
mov al,[bytearray] ; -> AL now is 6
mov ax,[wordarray] ; -> AX now is 4423
mov ax,[wordarray+2] ; -> AX now is 7724
mov ax,[wordarray+4] ; -> AX now is 3356
dec word [wordarray+2]
dec word [wordarray+2]
dec word [wordarray+2]
mov ax,[wordarray+2] ; -> AX now is 7724-3=7721
mov eax,0x08048084 ; -> EAX now is 0x08048084
mov ebx,[eax] ; -> EBX now is 1234
inc ebx ; -> EBX now is 1235
mov eax,ebx ; -> EAX now is 1235
mov [0x08048084], eax ; -> Save value of EAX at address 0x08048084
mov edx, [0x08048084] ; -> EDX now is 1235
inc dword [address] ; -> increase 32 bit value at address, so, after this operation data would be
Here you used [], so they have the equivalent effect of inc address right? (and address or register have equivalent effects?)
Yes, it can increase value that is stored in register OR
it can increase value that is stored at specified memory location.
Last question....you used dword..it means that you take all 32 bits and add 1 right?
Yes, it will add 1 to 32 bit number.
Let's say, we have 255 stored in address, 00000000 00000000 00000000 11111111
After inc dword address we have had 256, ->00000000 00000000 00000001 00000000<-
(all 32 bits)
Yes, 32bits: 00000000 00000000 00000001 00000000
OR it is 256.
byte is 8 bits:
If we were to use inc word address, we would have had 00000000 00000000 00000000 ->00000000<-
? (8 bits)
Yes, but word is 16 bits, byte is 8 bits.
word is 16 bits:
If we were to use hword address, we would have had 00000000 00000000 ->00000001 00000000<-
? (16 bits, however correct but just a case since here we do not overgo the 16 bits as we did in the 8 bit).
Yes, i dont know what is
hword, i think you mean
word because it's 16 bits.
So, you were mixing byte and word.
BYTE/WORD/DWORD/QWORD = 8/16/32/64 bits
Basically, know what type of data you'r dealing with and choose wisely your dword/word/ecc directives or you'll have wrong results?
Yes, correct!
Here is one more example:
segment .data
num: db 255 ; db - define byte
segment .code
inc byte [num] ; Increase byte at address "num". (255+1=0)
mov al,[num] ; AL is 8 bit register, so it loads one byte from address "num"
inc al ; AL now is 1
And that's it!
Code examples are good resource for learning,
but not all code examples are always correct.
Bye!