Brad
Brad

Reputation: 5

Problems using branching in Arm Assembly

So my code just displays both outputs like it is not branching to the then statement. I just started learning arm and I cant figure out what is going on and why it won't branch. Any help would be great as I have been looking at this for a total of about 5 hours with no progress.

The program is supposed to take a number as input and display if it is larger or less than 100

ldr r3, =intInput
ldr r3, [r3]
cmp r3, #100
bhs then

then:   
ldr r0, =strOutputNumLarger
bl printf

else:
ldr r0, =strOutputNumSmaller
bl printf

Upvotes: 0

Views: 493

Answers (1)

Weather Vane
Weather Vane

Reputation: 34585

The conditional branch bhs then goes to then: if the branch is taken, and also if it is not taken. There is no apparent branch to else:. Surely the first branch should be bhs else: and the messages swapped.

After the first branch and link there is another pair of similar instructions, at else:. So there should be an unconditional branch to prevent the then: code block falling through to the else: code block, by skipping over it.

So the code should be

ldr r3, =intInput
ldr r3, [r3]
cmp r3, #100
bhs else                       ; changed destination

then:
ldr r0, =strOutputNumSmaller   ; swapped messages
bl printf
b cont                         ; skip next code block

else:
ldr r0, =strOutputNumLarger
bl printf

cont:

Upvotes: 1

Related Questions