我如何用x86 NASM汇编语言打印地址?

问题描述 投票:1回答:1

我正在尝试在NASM x86组件中打印变量的地址。当我汇编此代码时,它会很好地汇编,但是当我运行此代码时,它将打印两个字符而不是地址。

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

屏幕截图:for your reference

linux assembly x86 nasm system-calls
1个回答
0
投票

您应该只将输出格式化为十六进制数字。您可以为此使用C中的printf

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
© www.soinside.com 2019 - 2024. All rights reserved.