简体   繁体   English

如何在NASM汇编程序中打印以下内容?

[英]How to print the following in NASM assembler?

I am still learning how to use NASM so there may be some syntax mistakes in my question. 我仍在学习如何使用NASM,因此我的问题中可能存在一些语法错误。 Anyway, when I create a label, suppose 无论如何,当我创建标签时,

buf: resb 16

Then, the label buf contains a memory address that points to the first of the 16 bytes that were reserved. 然后,标签buf包含一个内存地址,该地址指向保留的16个字节中的第一个字节。 Then, I want to print everything from an arbitrary byte in those 16 to the very last one, say, print the bytes 13 through 16. Then the code would look something like 然后,我要打印从那16个字节中的任意字节到最后一个字节的所有内容,例如,打印13至16字节。然后代码看起来像

mov eax, 4
mov ebx, 1
mov ecx, buf + 12
mov edx, 4         ;; for the 4 bytes to be read 
int 80h

Is this correct? 这个对吗? Then, what I am actually trying to do is 然后,我实际上想做的是

some_number:     resb 1

mov [some_number], byte 3

mov eax, 4
mov ebx, 1
mov ecx, buf + 16 - [some_number]
mov edx, byte [some_number]         ;; for the 3 bytes to be read, in this case 
int 80h

so I want to pass the address buf + (16 - 3) which is buf + 13. But this definitely does not work. 所以我想传递地址buf +(16-3)就是buf +13。但这绝对不起作用。 Let me know if this is wrong or if there is a better way to achieve this. 让我知道这是错误的还是有更好的方法来实现这一目标。

By the way, some_number will be determined by an arbitrary process so it will be different each time my program runs. 顺便说一句, some_number将由任意进程确定,因此每次我的程序运行时,它将有所不同。

mov ecx, buf + 12 is invalid, but you can achieve that by using lea ecx, [buf + 12] which is pretty much " load effective address of buf + 12 " and that's what you want. mov ecx, buf + 12无效,但是您可以使用lea ecx, [buf + 12]来实现,这几乎是“ buf + 12的加载有效地址 ”,这就是您想要的。

While the effective address format does allow registers, it doesn't allow indirection or subtraction, so lea ecx, [buf + 16 - [some_number]] would be doubly invalid. 有效地址格式虽然允许寄存器,但不允许间接或减法,因此lea ecx, [buf + 16 - [some_number]]将双重无效。 You will have to do the subtraction in a separate step, taking care to use the proper operand size. 您将必须在单独的步骤中进行减法,请小心使用正确的操作数大小。 One option would be: 一种选择是:

lea ecx, [buf + 16]
movzx eax, byte [some_number]
sub ecx, eax

If your some_number were a dword not a byte, you could simplify that to: 如果some_number是双字而不是字节,则可以将其简化为:

lea ecx, [buf + 16]
sub ecx, [some_number]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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