如何用 x86 NASM 汇编语言打印地址? [英] How do I print an address in x86 NASM assembly language?

查看:178
本文介绍了如何用 x86 NASM 汇编语言打印地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 NASM x86 程序集中打印 变量 的地址.当我组装这段代码时,它组装得很好,但是当我运行这段代码时,它会打印两个字符而不是地址.

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

截图:

推荐答案

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

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

这篇关于如何用 x86 NASM 汇编语言打印地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆