简体   繁体   中英

Why am I unable to print my number constant in NASM Assembly?

Learning NASM Assembly in 32-bit Ubuntu. I am somewhat confused:

In .bss , I reserve a byte for a variable:

num resb 1

Later I decided to give it a value of 5 :

mov byte [num],5

And at some point print it out:

mov EAX,4
mov EBX,0
mov ECX,num
add ECX,'0'   ; From decimal to ASCII
mov EDX,1
int 0x80

But it isn't printing anything.

I'm guessing that the problem is when I give num its value of 5 . I originally wanted to do this:

mov byte num,5

As I thought that num refers to a position in memory, and so mov would copy 5 to such position. But I got an error saying

invalid combination of opcode and operands

So basically, why is the program not printing 5? And also, why was my suggestion above invalid?

To print using int 0x80 and code 4 you need ECX to be the address of the byte to print. You added '0' to the address of num that was in ECX before you called the print routine, so it was the address of something else out in memory somewhere.

You may want something like this. I created a separate area, numout to hold the ASCII version of num :

numout resb 1
....

mov EAX,4
mov EBX,0
mov CL,[num]
add CL,'0'
mov [numout],CL
mov ECX,numout
mov EDX,1
int 0x80

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM