0

The purpose of this program is to take a linefeed terminated string (ASCII 10) and make it a null terminated string instead. I'm trying to print the null terminated string back out to console for confirmation but this is the behaviour I'm seeing:

pi@raspberrypi:~ $ ./tester

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

Here is the program, I traced through it but I don't see where the A is coming from. This is written on assembly for the 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
  • 1
    If `userInput` is only 4 bytes long, how do you intend to fit "1234" + \n into it? – David Wohlferd Apr 09 '21 at 01:08
  • Thank you, I changed the byte size to 6 and outputs fine now, any idea why the console line would display twice after hitting enter? – user460178 Apr 09 '21 at 01:23
  • The [Intro to Computer Organization, ARM Assembly on the Pi](https://bob.cs.sonoma.edu/IntroCompOrg-RPi/intro-co-rpi.html) is a good tutorial. Especially the later chapters and examples are helpful. – David C. Rankin Apr 09 '21 at 04:42
  • Thank you but that doesn't answer my question.... – user460178 Apr 09 '21 at 14:20
  • How many bytes are you reading? Are you leaving an extra \n in the buffer? – David Wohlferd Apr 10 '21 at 08:40
  • @DavidWohlferd I figured out what part I was missing, I was changing `userInput: .space 4` but forgot that I had to also change `MOV r2, #4` because the \n was being left in the buffer than read again by the console when it went to output the string. – user460178 Apr 10 '21 at 19:31

0 Answers0