首页 > 解决方案 > ARM 程序集,在将换行符(ASCII 10)更改为空终止并重新打印后打印出多余的字符(字母 A)

问题描述

该程序的目的是取一个以换行符结尾的字符串(ASCII 10)并将其改为以空字符结尾的字符串。我正在尝试将空终止的字符串打印回控制台以进行确认,但这是我看到的行为:

pi@raspberrypi:~ $ ./tester

Please enter 4 different numbers between 1-5 together without space or special characters. 
 
1234
1234A
pi@raspberrypi:~ $ 
pi@raspberrypi:~ $ 

这是程序,我追踪了它,但我看不到 A 来自哪里。这是写在 ARMv7 的程序集上

 .global _start

_start:
  LDR r1, =prompt
        BL _sPrint
    
        LDR     r1, =userInput      @ point to the space allocated for input
        MOV     r2, #4              @ set the limit of character to read in
        BL _sInput
        
        LDR r1, =userInput
        BL _sPrint
        
        Ldr r1, =newline
        BL _sPrint
        
        B _exit
    
    
    @_sPrint prints out a string based on it's variable length determined by _strlen
    @strlen, and findEnd are both needed for _sPrint.
    
    _sPrint:
        MOV r7, #4          @sets r7 to console STDOUT
        MOV r0, #1          @set WRITE destination to STDOUT (terminal)
        PUSH {r0, r1, lr}   
        BL _strLen          @gets the stringlength and the end
        POP {r0, r1, lr}    
        SWI 0
        mov pc, lr
    
    _strLen:
      mov   r2, #0
    
    findEnd:
     ldrb   r0, [r1], #1
      add   r2, r2, #1
      cmp   r0, #0
      bne findEnd
      sub   r2, r2, #1
      mov   pc, lr
    
    _sInput:
        PUSH {R1-R8, lr}
        MOV r7, #3          @register r7 being set to 3 to indicate message being read in (read syscall)
        MOV r0, #0          @Set READ device to the STDIN (keyboard)
        SWI 0
        POP {R1-R8, lr}
    
    @String fix takes a string value at r1's address and changes the line feed to be null termianted.   
    strfx:
        LDRB r0, [r1],#1    @loads a single byte from r1 (r1 is dereferenced), which is the _sInput to r0
        CMP r0, #10         @is r0 our newline?
        BNE strfx
        
        MOV r0, #0          @set r0 to null
        STRB r0, [r1, #-1]  @store r0's value back into r1's current address location. The final address 
        MOV PC, LR          @location of r1 newline to be the NULL in r1.

    _exit:
    MOV r7, #1
    SWI #0
    
 .data
    
    prompt:     .asciz "\nPlease enter 4 different numbers between 1-5 together without space or special characters. \n \n"
    newline:    .asciz  "\n"
    userInput:  .space 4

标签: assemblyarmarmv7

解决方案


推荐阅读