首页 > 解决方案 > NASM:变量没有改变

问题描述

我需要将第一个变量的值更改为第二个,但我编写的代码不起作用。我试过这个:

mov DWORD [enety_26500], enety_26501

但程序仍然打印我“你好,世界!”

任何人都可以帮忙吗?

        global _main
        extern _ExitProcess@4
        extern _printf
        section .text
_main:
        call main
        pop eax
        push    0
        call    _ExitProcess@4
        ret
print:
        push ebp
        mov ebp, esp
        mov eax, [ebp+8]
        push eax
        call _printf
        pop ebp
        push 0
        ret
main:
        push ebp
        mov ebp, esp
        push enety_26500
        call print
        pop eax
        mov DWORD [enety_26500], enety_26501
        push enety_26500
        call print
        pop eax
        pop ebp
        push 0
        ret

        section .data

        enety_26500:
                dw 'Hello, world!', 10, 0

        enety_26501:
                dw 'Hello,', 10, 0

标签: assemblynasm

解决方案


不清楚你想做什么。看起来您正在尝试这样做(在 C 中,使用更清晰的名称):

const char *stra = "hello world";
const char *strb = "hello";
...

printf(stra);
stra = strb;
printf(stra);

问题是,您不能让标签告诉您字符串的位置,然后尝试更改标签。这没有任何意义——标签本身不是容器或位置,它只是……一个标签。将标签存储在 eax 中并更改 eax 或将标签存储在另一个变量中:

    push    DWORD PTR sptr
    call    printf
    mov     eax, strb
    mov     DWORD PTR sptr, eax

    push    DWORD PTR sptr
    call    printf
    ....

sptr:
    .long   stra
stra:
    .string "hello world"
strb:
    .string "hello"

反正就是这样!

编辑:只是出于兴趣:如果您想更改 stra,并且 stra 存储在可写数据段中(即:.data),您可以这样做:

mov     BYTE PTR stra+5, 0

这将在“hello”部分之后写一个 0 并在该点终止字符串。直接在 stra 上调用 printf 会打印“hello”。


推荐阅读