How do I print an address in x86 NASM assembly language? [duplicate]

拜拜、爱过 提交于 2021-01-27 18:53:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!