Grevak
Grevak

Reputation: 543

How can I print numbers in my assembly program

I have a problem with my assembly program. My assembly compiler is NASM. The source and the outputs are in this picture:

enter image description here

The problem is that I can't print numbers from calculations with the extern C function printf(). How can I do it?

The output should be "Ergebnis: 8" but it isn't correct.

Upvotes: 0

Views: 1777

Answers (1)

SergeyLebedev
SergeyLebedev

Reputation: 3708

In NASM documentation it is pointed that NASM Requires Square Brackets For Memory References. When you write label name without bracket NASM gives its memory address (or offset as it is called sometimes). So, mov eax, val_1 it means that eax register gets val_1's offset. When you add eax, val_2, val_2 offset is added to val_1 offset and you get the result you see.

Write instead:

mov eax, [val_1]
add eax, [val_2]

And you shoul get 8 in eax.

P.S. It seems that you have just switched to NASM from MASM or TASM. There are a lot of guides for switchers like you. See for example nice tutorials here and here.

Upvotes: 4

Related Questions