我目前正在为我的计算机体系结构类的一个项目继续工作,该项目中将后缀表达式转换为完全带括号的中缀表达式。但是,在运行程序时,我很难找到如何遇到分段错误。我知道它与我的子程序readPostfix直接相关,后者调用方法append,后者调用lengthIs方法。我应该如何解决我面临的这个问题?
我尝试省略使用push来保存寄存器的值以尝试修复程序,但是我认为通过该操作我将丢失ecx的值,这是循环所必需的,以及其他一些重要的内容。
这是我想正确运行的代码部分。如果运行正常,则应输出A + B ...
%include "asm_io.inc"
segment .data
postfix db "AB+", 0
maxSize equ 100
segment .bss
postLength resb 1
segment .text
global _asm_main
_asm_main:
enter 0,0 ; setup routine
pusha
push postfix
call lengthIs
add esp,4
mov [postLength],eax
call print_int
push postLength
push postfix
call postfixToInfix
add esp, 8
popa
mov eax, 0 ; return back to C
leave
ret
; A subprogram that converts postfix expression to a fully parenthesized infix expression
segment .data
segment .bss
operator resd 1
segment .text
postfixToInfix:
push ebp
mov ebp, esp
mov edx, [ebp + 8]
mov ecx, [ebp + 12]
mov esi, 0
infixLoop:
mov al,[ebx + esi]
inc esi
cmp eax,'+'
je operation
cmp eax, ' '
je infix_exit
push eax
inc esi
loop infixLoop
operation:
mov [operator], al
pop ebx
pop edx
push eax
push ecx
push edx
push esi
push esp
push operator
push ebx
push 1
call append
add esp, 12
push ebx
push 1
call append
add esp, 12
pop esp
pop esi
pop edx
pop ecx
pop eax
push ebx
infix_exit:
inc esi
loop infixLoop
pop eax
call print_string
pop ebp
ret
; A subprogram that gets the length of the postfix expression
segment .text
lengthIs:
push ebp
mov ebp, esp
mov eax, 0
mov ebx, [ebp + 8]
mov esi, 0
mov ecx, maxSize
length_loop:
cmp [ebx + esi], dword 0
je length_exit
inc eax
inc esi
loop length_loop
length_exit:
pop ebp
ret
; A subprogram strcat appends the contents of one string to the end of another
; strcat(str1,str2)
; Result: str1= str1 + str2
segment .bss
append_length resd 1
segment .text
append:
push ebp
mov ebp, esp
mov edx, [ebp + 16]
push edx
call lengthIs
add esp, 4
mov [append_length], eax
mov eax, 0
mov ebx, [ebp + 12]
mov edx, [ebp + 16]
mov ecx, [append_length]
mov edi, 0
mov esi, [ebp + 8]
append_loop:
mov al, [edx + edi]
mov [ebx + esi], al
add esi, 1
add edi, 1
loop append_loop
pop ebp
ret