将堆栈中的弹出字符存储在内存中

时间:2017-11-17 00:46:20

标签: assembly armv7

我正在开发一个Armv7可装配程序,它要求用户键入字符,将它们存储到内存中,然后将它们推入堆栈。按下第4个字符后,程序弹出最后2个字符,将它们存储到内存中,然后打印出来。我有以下代码,但我无法弄清楚如何将弹出的字符存储到内存中。有什么建议吗?

.global _start
_start:

loop:
    add r4, #1          @increment the loop by 1
    cmp r4, #4          @compare r4 to the number 4
    bgt pop1            @branch if r4 is bigger than 4

    mov r7, #3          @read syscall
    mov r0, #0          @stdin file pointer
    mov r2, #2          @2 character, the character itself and the new line
    ldr r1, =string         @load the address of the input to r1
    swi 0

    ldrb r5, [r1]           @load the bit in address r1 to r4
    cmp r5, #'q'            @compare that character to the character q
    beq _exit           @branch if the character is q
    stmfd sp!, {r5}         @push the character to the stack
    bal loop            @branch back to the top of the loop
/*
nextline:
    mov r7, #4
    mov r0, #1
    mov r2, #1
    ldr r6, =newline
    swi 0 */
pop1:
    add r8, #1
    cmp r8, #2
    bgt loop
    ldmfd sp!, {r5}         @PROBLEMs
    str r5, [r9]
    @bal pop1
print:
    mov r7, #4
    mov r0, #1
    mov r2, #2
    ldr r1, =string
    swi 0

    @bal pop1

_exit:
    mov r7, #1
    swi 0



    .data
string:
    .space 2

newline:
    .ascii "\n"
store:
    .space 2

现在,代码在到达str r5时会产生分段错误,[r9]

0 个答案:

没有答案