I'm working with the following code:
section .text
global _start
_start:
mov ebx, testing
mov [ebx], byte 0x4A
add ebx, byte 1
mov [ebx], byte 0x4B
add ebx, byte 1
mov [ebx], byte 0x4C
;sets var1 as '*'
mov eax, 0x2A
mov [var1], eax
;sets var2 as 'N'
mov eax, 0x4E
mov [var2], eax
;sets var3 = var2
mov eax, [var2]
mov [var3], eax
;Prints var1
mov eax, 4
mov ebx, 1
mov ecx, var1
mov edx, 1
int 0x80
;Jumps a line
mov eax, 4
mov ebx, 1
mov ecx, line_break
mov edx, 2
int 0x80
;Prints var2
mov eax, 4
mov ebx, 1
mov ecx, var2
mov edx, 1
int 0x80
;Jumps a line
mov eax, 4
mov ebx, 1
mov ecx, line_break
mov edx, 2
int 0x80
;Prints var3
mov eax, 4
mov ebx, 1
mov ecx, var3
mov edx, 1
int 0x80
;Jumps a line
mov eax, 4
mov ebx, 1
mov ecx, line_break
mov edx, 2
int 0x80
;Prints variable
mov eax, 4
mov ebx, 1
mov ecx, variable
mov edx, 3
int 0x80
;Jumps a line
mov eax, 4
mov ebx, 1
mov ecx, line_break
mov edx, 2
int 0x80
;Prints testing
mov eax, 4
mov ebx, 1
mov ecx, testing
mov edx, 3
int 0x80
;Jumps a line
mov eax, 4
mov ebx, 1
mov ecx, line_break
mov edx, 2
int 0x80
;Finishes program
mov eax, 1
mov ebx, 1
int 0x80
section .data
variable times 3 db 0x2A
line_break dw 0x0D0A
section .bss
testing resb 3
var1 resb 1
var2 resb 1
var3 resb 1
It prints out:
*
N
N
***
JKL
What i don't understand is: why in the first line "mov ebx, testing" there is no brackets arround "testing" and why those brackets are present in [var2] in the line "mov eax, [var2]". How exactly does this notation work? Shouldn't [var2] be the effective address of var2?
The brackets around an identifier indicates that the value should be used as a pointer. So without the brackets:
mov ebx, testing
moves the address of testing
into the eax
register, where
mov eax, [var2]
moves the value pointed to by var2 into eax
.
why in the first line "mov ebx, testing" there is no brackets arround "testing"
The code in your question is written in NASM syntax. There are other x86 assemblers that use a slightly different syntax (eg MASM and TASM).
Anyway, mov ebx, testing
means "place the address of testing
in register ebx
" . That address is then used on the very next line, where the instruction mov [ebx], byte 0x4A
writes the value 0x4A
to the first byte pointed to by ebx
(ie the first byte at testing
).
In MASM/TASM syntax you would have written this as mov ebx, OFFSET testing
.
why those brackets are present in [var2] in the line "mov eax, [var2]"
Because here you want to get the value at var2
, not var2
's address. And in NASM syntax you do that using brackets, as in mov eax,[var2]
. In MASM/TASM syntax both mov eax,[var2]
and mov eax,var2
would've read the value at var2
.
Note that var1
, var2
and var3
are one byte each. So trying to read and write 32-bit registers from/to those variables looks suspiciously like a bug. You might want to change the declarations from resb 1
to resd 1
, or just read/write 8-bit registers from/to them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.