ARM Assembly, register not being set to 0?

0
start

read

    BL  getkey              ; read key from console
    CMP R0, #0x0D           ; while (key != CR)
    BEQ endRead             ; {
    BL  sendchar            ;   echo key back to console    

    CMP R0, #' '            ; if (ro = ' ')
    BNE nocount             ;{                          
    ADD R7, R7, #1          ; r7 += 1
    LDR R4, =0              ; r4 = 0
nocount                     ;}

    MOV R6, R0              ; Store input in R6
    SUB R6, R6, #'0'        ; Convert from decimal to hex
    MULS R4, R5, R4         ; If there is another input, multiply total by 10
    ADDS R4, R4, R6         ; Add the input to the total

    ADD R8, R4, R8          ;sum

    B   read    

endRead                 

end                     

stop    B   stop

    END 

Hi, I'm trying to have my code so when there is a space entered into the console, 1 will be added to R7 and R4 will be set to 0.

When a space is added, R4 is set to 0xFFFFFFF0, but when I add '5' to this, I get 0xFFFFFFF65. I expect to just get 0x00000005. This is weird, because when I put the same command outside the compare, it works. Any help is appreciated, thanks.

Also I want r8 to be the sum of numbers entered, it doesn't seem to be working properly, can anyone tell me why?

assembly
arm
cpu
cpu-registers
asked on Stack Overflow Nov 15, 2016 by Seamus W • edited Nov 15, 2016 by Michael

4 Answers

1

Have you explored conditional execution ADD, etc?

Assumes: ARMv7 My machine = Linux raspberrypi 4.4.26-v7+ #915 SMP Thu Oct 20 17:08:44 BST 2016 armv7l GNU/Linux

        cmp r0, #0x20          // white space - space char
        addeq r7, r7, #1        // conditional add
        eoreq r4, r4            // conditional zero
answered on Stack Overflow Nov 15, 2016 by InfinitelyManic
0
LDR R4, =0

doesn't assign zero to the register. It loads the register from the memory address 0. Use

MOV R4, #0

instead.

answered on Stack Overflow Nov 15, 2016 by Seva Alekseyev
0

Regarding the summation of the numbers; please the example below of a simple decremented loop where the sum of each number is added to R1. Hope this helps.

main:   nop

        eor r2, r2
        mov r1, #0xf
        1:

        add r2, r2, r1
        bl write

        subs r1, #1
        bne 1b

OUTPUT:

15 15
14 29
13 42
12 54
11 65
10 75
9 84
8 92
7 99
6 105
5 110
4 114
3 117
2 119
1 120
answered on Stack Overflow Nov 15, 2016 by InfinitelyManic
0

Sorry everyone, the code is correct, it was just not in the right position, thanks everyone!

answered on Stack Overflow Nov 15, 2016 by Seamus W

User contributions licensed under CC BY-SA 3.0