简体   繁体   English

NASM 中 STOSB 的分段错误

[英]Segmentation fault for STOSB in NASM

I am trying to write a subroutine that takes in a string, looks at each letter, and replaces lowercase vowels with uppercase vowels.我正在尝试编写一个子例程,它接受一个字符串,查看每个字母,并将小写元音替换为大写元音。 I am using raspberry pi desktop (x86) on a VM with NASM.我在带有 NASM 的虚拟机上使用树莓派桌面 (x86)。 Here is part of my code:这是我的代码的一部分:

again:
lodsb ; load next byte into AL and increment EIP
cmp AL, 0 ; check for end
jz quitloop ; exit if end
cmp AL, 'a' ; check if char is a
jnz next1 ; jump to next test if not a
dec ESI ; move back to address of character
mov AL, 'A' ; replace character
stosb ; store character
jmp again ; restart loop with next char

"next1" checks for 'e' and on until y. “next1”检查 'e' 直到 y。 From what I can tell, lodsb seems to be working because for a string starting with "the" it loops through all tests twice then gets a segmentation error in test1 (checking the e).据我所知,lodsb 似乎可以正常工作,因为对于以“the”开头的字符串,它会循环遍历所有测试两次,然后在 test1 中出现分段错误(检查 e)。 The documentation I can find on STOSB is not that helpful;我在 STOSB 上找到的文档没有多大帮助; it says I can use parameters but not how to do so.它说我可以使用参数,但不知道如何使用。 (If I try to put registers as parameters, it doesn't assemble because of operand/operator error.) (如果我尝试将寄存器作为参数,它不会因为操作数/运算符错误而汇编。)

lodsb; lodsb; load next byte into AL and increment EIP将下一个字节加载到 AL 并递增EIP

It's not fruitful to state that this instruction increments EIP.该指令递增 EIP 对 state 没有结果。 Every instruction has an effect on EIP.每条指令都会对 EIP 产生影响。 Maybe you meant to say that lodsb increments ESI?也许您的意思是说lodsb会增加 ESI?


Seeing that dec ESI instruction, I assume that you're working in 32-bit mode where DS would normally be equal to ES .看到dec ESI指令,我假设您在DS通常等于ES的 32 位模式下工作。

The lodsb string primitive works from DS:ESI and stosb works from ES:EDI . lodsb字符串原语从DS:ESI开始工作,而stosbES:EDI开始工作。

If your intention is to capitalize in-place, then the simple solution is to write the capital vowel at the address right before where ESI is pointing to:如果您打算就地大写,那么简单的解决方案是在 ESI 指向的地址之前写上大写元音:

again:
lodsb                   ; load next byte into AL and increment EIP
cmp AL, 0               ; check for end
jz quitloop             ; exit if end
cmp AL, 'a'             ; check if char is a
jnz next1               ; jump to next test if not a

mov byte [ESI - 1], 'A' ; replace character

jmp again               ; restart loop with next char

Please notice that the dec ESI instruction together with not effectively changing the small character, provoked an endless loop!请注意, dec ESI指令与未有效更改小字符一起引发了无限循环!
And because the stosb instruction happily ran along, at some point EDI will have triggered a segmentation fault.并且因为stosb指令愉快地运行,在某个时候 EDI 将触发分段错误。

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

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