1

I'm working on this armv7 assembly program that finds the greatest common divisor(gcd) of two integers. Everything is working fine except for the newline function. When i assemble and run the program, it doesn't print any newlines, just the integers in one line. Any suggestions on how i can fix that?

  .global _start
_start:
    mov r2, #24     @first set of integers
    mov r4, #18
    bl mysub1
    bl mysub2
    bl mysub3

    mov r2, #78     @second set of integers
    mov r4, #34
    bl mysub1
    bl mysub2
    bl mysub3

    mov r2, #99     @third set of integers
    mov r4, #36
    bl mysub1
    bl mysub2
    bl mysub3

_exit:
    mov r7, #1
    swi 0


mysub1:             @subroutine to find gcd
    cmp r2, r4
    beq done
    bgt greater
    blt less
greater:
    sub r2, r2, r4
    bal mysub1

less:
    sub r4, r4, r2
    bal mysub1
done:
    bx lr

mysub2:             @subroutine to convert gcd result to ascii value
    add r4, #48
    ldr r9, =store
    str r4, [r9]

    mov r7, #4      @print out a newline
    mov r0, #1
    mov r2, #1
    ldr r1, =newline
    swi 0

    bx lr

mysub3:             @subroutine to print out the ascii value
    mov r7, #4
    mov r0, #1
    mov r2, #2
    ldr r1, =store
    swi 0

    bx lr


    .data
store:
    .space 2

newline:
    .ascii "\n"
fuz
  • 88,405
  • 25
  • 200
  • 352
Breezyb12
  • 37
  • 1
  • 9
  • What operating system are you programming for? You could have really given your functions more descriptive names. `mysub1`, `mysub2`, and `mysub3` are quite useless names. – fuz Nov 26 '17 at 20:10
  • @fuz I'm just using a raspberry pi that operates on raspbian. I know the mysubs don't make sense, that's why I put comments beside them. – Breezyb12 Nov 26 '17 at 20:15
  • Try to rename your functions instead! – fuz Nov 26 '17 at 20:16
  • @fuz that didn't fix it! – Breezyb12 Nov 26 '17 at 20:21
  • Renaming functions is about making the code easier to understand for others, not directly about fixing bugs. – fuz Nov 27 '17 at 08:47

1 Answers1

1

This is the culprint:

add r4, #48
ldr r9, =store
str r4, [r9]

This code has two bugs:

  • it only works for numbers between 0 and 9
  • str r4, [r9] stores four bytes to store, overwriting the newline right after the two-byte buffer.

To fix the first issue, you need to do a division with rest to separate the number in r4 into two digits. To fix the second issue, use strb or strh to store a byte or halfword instead as to not overrun the buffer.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • 1
    @Breezyb12 Basically, you divide `r4` by ten. The quotient is your first digit, the remainder is your second digit. If you have more than two digits, you need to divide multiple times. See [this answer](https://stackoverflow.com/questions/8348030/how-does-one-do-integer-signed-or-unsigned-division-on-arm) for how to do this if your CPU doesn't have the `udiv` or `sdiv` instruction. – fuz Nov 26 '17 at 20:37
  • got it, Thank you so much! – Breezyb12 Nov 26 '17 at 20:51