0

我无法弄清楚如何在中缀表达式中添加空格,以便像 3+5 这样的东西会变成 3+5。我不断收到分段错误,我什至无法确定错误来自哪一行,因为由于某种原因,我也无法打印出任何内容。这是我的代码供参考:

.data
 
.balign 4
return: .word 0
 
.balign 4
operations: .asciz "+-*/^"
 
.balign 4
    output: .space 100
 
.balign 4
    spa: .asciz " "
.text
.global printf
.global main
 
main:
    ldr r0, [r1, #4] //load address of first character in expression
    ldr r1, =return
    str lr, [r1] //store lr in return variable
    ldr r2,=output //load address of output on r2
    ldr r3, =spa //load address of space variable
    ldr r3, [r3] //load character ' ' onto r3
 
loop:
    cmp r0, #0 //check if reached end of input
    beq end //if so go to end
    ldr r1, =operations //load the address of  operations string in r1
 
loop2:;
    cmp r1, #0 //check if reached end of operation string
    beq loop3 //means the current character in output isn't an operator
    ldr r5, [r1] //else load value of r1 in r5, the current operator
    ldr r6, [r0] //load current value in input string to r6
    cmp r5, r6 //compare the two values
    beq loop3 //if equal go to loop3
    add r1, r1, #1 //if not increment address of r1
    b loop2 //start next iteration of loop2
 
loop3:
    str r3, [r2] //since current character is an operator, store space first
    add r2, r2, #1
    b loop4 //on output string. Then go to loop4
 
 
loop4:
    str r6, [r2] //store the current character, stored in r6, in r2
    add r0, r0, #1 //increment address of r0 by 1 to get to next character
    add r2, r2, #1 //increment address of r2 by one to get to next slot in array
    b loop //start next iteration of loop
 
end:
    ldr r0, =output //load address of output onto r0
    bl printf //print out string
    ldr lr, =return //restore link register
    ldr lr, [lr]
    bx lr
4

0 回答 0