简体   繁体   中英

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. 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. 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). The documentation I can find on STOSB is not that helpful; 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; load next byte into AL and increment EIP

It's not fruitful to state that this instruction increments EIP. Every instruction has an effect on EIP. Maybe you meant to say that lodsb increments ESI?


Seeing that dec ESI instruction, I assume that you're working in 32-bit mode where DS would normally be equal to ES .

The lodsb string primitive works from DS:ESI and stosb works from ES: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:

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!
And because the stosb instruction happily ran along, at some point EDI will have triggered a segmentation fault.

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