【发布时间】:2020-09-16 00:47:47
【问题描述】:
我试图在数据传入后仅将第一个字节存储在寄存器中 例如,如果我有以下 ASM 代码
global _start
section .data
string db "Hello", 0x00 ; string = Hello
section .text
_start:
mov eax, string ; move "Hello" into eax (just to store it for now)
这就是我被卡住的地方,我只想将“H”存储到 eax 中,例如,如果我将“H”移动到 EAX,然后将 eax 移动到 edx,因此 EDX =“H”,我可以这样做:
mov ebx, 1
mov ecx, edx ; This is so I can store "H" in ecx
mov edx, 12 ; 12 is longer than the string, however, I am just using this to test, as it should only output "H", rather than anything else, the reason for this is later I want to use "H" in a cmp statement
int 0x80 ; execute the system call
现在我也尝试使用 al,例如:mov [edx], al 将字符串移动到 eax 后,但这仍然会输出完整的字符串,如果可以为此提供工作示例,将不胜感激
【问题讨论】:
-
mov [edx], al将 AL 中的字节存储到 EDX 指向的内存位置。这已经很遥远了。 -
请注意,写系统调用需要一个指向缓冲区的指针。将字符加载到寄存器中时,它不会像您期望的那样。
-
@SevaAlekseyev 如果我这样做
global _start section .data string db "testing", 0x00 section .text _start: xor eax, eax mov eax, string mov [edx], al mov eax, 4 mov ebx, 1 mov ecx, edx mov edx, 12 int 0x80 mov eax, 1 mov ebx, 0 int 0x80它返回一个段错误,有什么想法吗? -
我的眼睛。下次编辑问题。无论如何,在
mov [edx],al中,您尝试将一个字节写入 EDX 指向的内存位置。 EDX 包含垃圾。那是你的段错误。 -
相反的问题,加载一个字节,是How to load a single byte from address in assembly(这里的Seva的回答似乎是在回答这个问题。)
标签: assembly memory-management x86 nasm