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