这可能是我学习x86汇编语言的最后一个障碍。
下面的子程序给了我一个分段错误:
;=================================================================
; RemCharCodeFromAToB - removes all chars between a and e from str
; arguments:
; str - string to be processed
; a - start
; e - end
; return value:
; n/a
;-------------------------------------------------------------------
RemCharCodeFromAToB:
; standard entry sequence
push ebp ; save the previous value of ebp for the benefi$
mov ebp, esp ; copy esp -> ebp so that ebp can be used as a $
; accessing arguments
; [ebp + 0] = old ebp stack frame
; [ebp + 4] = return address
mov edx, [ebp + 8] ; string address
while_loop_rcc:
mov cl, [edx] ; obtain the address of the 1st character of the string
cmp cl, 0 ; check the null value
je while_loop_exit_rcc ; exit if the null-character is reached
mov al, cl ; save cl
mov cl, [ebp + 16] ; end-char
push cx ; push end-char
mov cl, [ebp + 12] ; start-char
push cx ; push start-char
push ax; ; push ch
call IsBetweenAandB
add esp, 12
cmp eax, 0 ; if(ch is not between 'a' and 'e')
je inner_loop_exit_rcc
mov eax, edx ; copy the current address
inner_loop_rcc:
mov cl, [eax+1]
cmp cl, 0
je inner_loop_exit_rcc
mov [eax], cl
inc eax
jmp inner_loop_rcc
inner_loop_exit_rcc:
inc edx ; increment the address
jmp while_loop_rcc ; start the loop again
while_loop_exit_rcc:
; standard exit sequence
mov esp, ebp ; restore esp with ebp
pop ebp ; remove ebp from stack
ret ; return the value of temporary variable
;===================================================================我怀疑从32位到8位寄存器的数据转换有什么问题,反之亦然。我对此的概念还不清楚。
或者,下面的部分有什么问题吗?
mov al, cl ; save cl
mov cl, [ebp + 16] ; end-char
push cx ; push end-char
mov cl, [ebp + 12] ; start-char
push cx ; push start-char
push ax; ; push ch
call IsBetweenAandB
add esp, 12发布于 2019-04-07 02:28:40
cx和ax是16位寄存器,所以push cx ; push cx; push ax在堆栈上推送16位值,总共有6个字节。但是IsBetweenAandB显然期望32位值,并且在esp末尾添加12 (而不是6)。所以你可能想要push ecx等等。
此外,在使用eax和ecx之前,您可能希望将它们调零。就目前情况而言,它们可能最初包含垃圾,并且只将有用的数据加载到低8位的al和cl中。因此,当IsBetweenAandB试图比较整个32位值时,您将得到错误的结果。否则,您希望重写IsBetweenAandB,以便只比较您所关心的低字节。
https://stackoverflow.com/questions/55554890
复制相似问题