问题
I am trying to print address of variable in NASM x86 assembly. When I assemble this code it assembles fine, however when I run this code it prints two characters instead of the address.
section .bss
Address: RESB 4
section .data
variable db 1
section .text
global _start
_start:
mov eax , variable ; variable Address is stored in eax register
mov [Address] , dword eax ; move the value of eax to Address
mov eax , 4 ; write system call in linux
mov ebx , 1 ; stdout file descriptor
mov ecx , Address ; memory address to be printed.
mov edx , 4 ; 4 bytes to be print
int 0x80
mov eax , 1
int 0x80
screenshot:
回答1:
You should just format the output as hex number. You can use printf from C for this purpose
extern printf
section .bss
Address: RESB 4
section .data
variable db 1
fmt db "0x%x", 10, 0 ; format string
section .text
global _start
_start:
mov eax , variable ; variable Address is stored in eax register
mov [Address] , dword eax ; move the value of eax to Address
push dword [Address] ; push value of Address
push dword fmt ; address of format string
call printf ; calling printf
add esp, 8 ; pop stack 2*4 bytes after passing two variables to printf
mov eax, 0 ; exit code 0
int 0x80
来源:https://stackoverflow.com/questions/47567621/how-do-i-print-an-address-in-x86-nasm-assembly-language