为什么我无法在NASM程序集中打印我的数字常量?

时间:2022-09-03 23:23:23

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

在32位Ubuntu中学习NASM程序集。我有点困惑:

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

在.bss中,我为变量保留一个字节:

num resb 1

Later I decided to give it a value of 5:

后来我决定给它一个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:

我猜这个问题是当我给num的值为5.我原本想要这样做:

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

因为我认为num指的是内存中的位置,所以mov会将5复制到这样的位置。但是我说错了

invalid combination of opcode and operands

操作码和操作数的无效组合

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

所以基本上,为什么程序不打印5?而且,为什么我的建议无效?

1 个解决方案

#1


1  

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.

要使用int 0x80和代码4进行打印,需要ECX作为要打印的字节的地址。在调用打印例程之前,您在ECX中的num地址中添加了“0”,因此它是内存中某些内容的地址。

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

你可能想要这样的东西。我创建了一个单独的区域,numout来保存num的ASCII版本:

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

#1


1  

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.

要使用int 0x80和代码4进行打印,需要ECX作为要打印的字节的地址。在调用打印例程之前,您在ECX中的num地址中添加了“0”,因此它是内存中某些内容的地址。

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

你可能想要这样的东西。我创建了一个单独的区域,numout来保存num的ASCII版本:

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