danidoco
danidoco

Reputation: 77

How to change variable in NASM (x86, Microsoft Windows 10)

I want to change the value of a variable in NASM.

I searched the internet for a long time and I tried a lot of things.

I've tried this:

global  _main
extern  _printf

section .data
original: db "Hello, world", 10, 0
new: db "Hello", 10, 0

section .text
_main:
    mov [original], new

    push original
    call _printf
    add esp, 4

    ret

This does not works, so I've tried this.

global  _main
extern  _printf

section .data
original: db "Hello, world", 10, 0
new: db "Hello", 10, 0

section .text
_main:
    mov eax, new
    mov [original], eax

    push original
    call _printf
    add esp, 4

    ret

Well, it works without errors, but it keeps printing weird things.

@@

(Actually, there were arrow-shaped characters pointing up and down at the beginning of the string above, but I omitted it because it did not seem to be recognized)

I've tried and searched a lot of things. But I couldn't find a solution. So how can I change the value of a variable in NASM? (I'm using x86 assembly in Windows)

Thanks.

Upvotes: 0

Views: 562

Answers (1)

prl
prl

Reputation: 12452

You cannot overwrite a string using a single instruction. You need to use a loop to copy the bytes of the string, while checking for the null byte that marks the end of the string. For example:

        mov edi, original
        mov esi, new
.loop:
        mov al, [esi]
        mov [edi], al
        add esi, 1
        add edi, 1
        test al, al
        jnz .loop

Upvotes: 2

Related Questions