0

I'm trying to make a assembly program to add two numbers, but when I type in the numbers it includes the \n at the end of the script meaning that when I do

add R3,R2,R1 

it includes \n as a number when I run strace it it shows that the program is printing two null terminators \0\0 picture of the program running and the program running with strace

the code is as follows

    .data
    keyin: .space 20
    num1: .space 2
    num2: .space 2
    num3: .space 3
    addmsg1: .ascii "enter a number: "
    addmsg2: .ascii "enter another number: "
    addmsg3: .ascii "the sum of those numbers are: "
    addcode: .ascii "add\n"

    .text
    .global _start:
_start:
    ldr R1,=addmsg1
    mov R2,#16
    bl print

    ldr R1,=num1
    mov R2,#2
    bl input

    ldr R1,=addmsg2
    mov R2,#22
    bl print

    ldr R1,=num2
    mov R2,#2
    bl input

    ldr R1,=num1
    ldr R1,[R1]
    ldr R2,=num2
    Ldr R2,[R2]
    add R3,R2,R1
    ldr R2,=num3
    str R3,[R2]
    ldr R1,=addmsg3
    mov R2,#30
    bl print

    ldr R1,=num3
    mov R2,#2
    bl print

print:
    mov R7,#4
    mov R0,#1
    SWI 0
    bx lr
    
input:
    ldr R1,=keyin
    mov R7,#3
    mov R0,#0
    SWI 0
    bx lr

I don't want to keep the \n when I type in the number but I have to accept it as part of the input otherwise if I typed 3 for the first number the \n would go into the second number as the input

I want to shorten the string to just one byte so that only the numbers are present

im uing rpi 4

arm V8-a raspbian buster (for the syscalls)

any help would be welcome if you need more info just ask

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • 1
    You shouldn't be using `ldr` to load from something declared with `.space 2`. If you only care about the first byte, then why don't you use `ldrb` instead? – Michael Jun 23 '20 at 13:42
  • Also, your current way of calculating the sum is incorrect. You're dealing with characters, so you need to subtract `'0'` after the addition. Note that this will only work when both the inputs and the sum are single digits. – Michael Jun 23 '20 at 13:47
  • [Why won't the program print the sum of the array?](https://stackoverflow.com/a/53252729) explains why adding ASCII codes needs a `sub r0, #'0'` at the end. But the code in that Q&A is for x86 so not an exact duplicate. – Peter Cordes Jun 23 '20 at 16:00
  • i tried doing the ldrb instead but the i got segmentation fault – blankettripod32 Jun 24 '20 at 09:02

0 Answers0