以下程序的行为非常奇怪。
执行后,\n
会自动附加在名为str_low
的字符串的末尾。
我已经从_main
的第一行开始调试,并使用以下命令检查了字符串的结束地址:
print *(char *) 0x......
,发现最后一个字符是\n
而不是\0
。
可能是什么原因?
extern _printf
extern _putchar
section .data
str_form: db "%s", 10, 0
str_low: db "abxyz", 10, 0
section .text
global _main
_main:
mov ebp, esp; for correct debugging
push ebp
mov ebp, esp
push str_low
call _reversepairs
add esp, 4
push str_low
call _PrintString
add esp,4
mov esp, ebp
pop ebp
ret
_reversepairs:
; 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 $
sub esp, 4 ; create 1 local variables
;--------------------------;
; [ebp+8]== string-address ;
;--------------------------;
xor eax, eax
mov eax, dword[ebp+8]; obtain string address
;-------------------------;
; [ebp-4] == loop counter ;
;-------------------------;
mov [ebp-4], eax ; save address for loop counting
while_cs:
mov eax, dword[ebp-4];obtain current char address
xor ecx, ecx
mov cl, byte[eax];char ch = str[i];
cmp ecx, 0; ch <?> '\0'
je while_exit_cs ; if(ch == '\0') break;
; char cl = newString[i + 1];
mov eax, dword[ebp-4]; obtain current char address
xor ecx, ecx
mov cl, byte[eax+1]; obtain next char
cmp cl, 0
je while_exit_cs
xor edx, edx
mov dl, byte[eax]; obtain current char
mov byte[eax+1], dl ; ------
mov byte[eax], cl ; swap
increment_i:
mov eax, dword[ebp-4]
inc eax ;------
inc eax ; i=i+2;
mov dword[ebp-4], eax
mov eax, dword[ebp-4]; make sure char was assinged properly
jmp while_cs ; iterate through while
while_exit_cs:
xor eax, eax
mov eax, dword[ebp+8]; return str address
; exit sequence
add esp, 4 ; destroy local variable
mov esp, ebp ; restore esp with ebp
pop ebp ; remove ebp from stack
ret ; return the value of temporary variable
_PrintString:
push ebp
mov ebp, esp
mov eax, dword[ebp+8]
push eax
push str_form
call _printf
add esp, 8
mov esp, ebp
pop ebp
ret