[英]I'm learning assembly on linux, and [] notation is confusing me. (NASM)
我正在使用以下代碼:
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
它輸出:
*
N
N
***
JKL
我不明白的是:為什么在第一行“ mov ebx,testing”中沒有括號“ testing”,以及為什么這些括號出現在“ mov eax,[var2]”行的[var2]中。 這種表示法是如何工作的? [var2]不應該是var2的有效地址嗎?
標識符周圍的方括號表示該值應用作指針。 因此,沒有括號:
mov ebx, testing
將testing
地址移到eax
寄存器中
mov eax, [var2]
將var2指向的值移動到eax
。
為什么在第一行“ mov ebx,測試”中沒有方括號“測試”
您問題中的代碼是用NASM語法編寫的。 還有其他x86匯編器使用略有不同的語法(例如MASM和TASM)。
無論如何, mov ebx, testing
意味着“將testing
地址放置在寄存器ebx
” 。 然后在下一行使用該地址,指令mov [ebx], byte 0x4A
將值0x4A
寫入ebx
指向的第一個字節(即, testing
的第一個字節)。
在MASM / TASM語法中,您應該將其編寫為mov ebx, OFFSET testing
。
為什么這些括號出現在“ mov eax,[var2]”行的[var2]中
因為在這里您要獲取var2
的值,而不是var2
的地址。 在NASM語法中,您可以使用方括號來完成此操作,例如mov eax,[var2]
。 在MASM / TASM語法中, mov eax,[var2]
和mov eax,var2
都將讀取var2
處的值。
注意var1
, var2
和var3
均為一個字節。 因此,嘗試從這些變量讀取32位寄存器或向這些變量寫入32位寄存器看起來像是一個可疑的bug。 您可能需要將聲明從resb 1
更改為resd 1
,或僅從它們讀取/ resd 1
寫入8位寄存器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.