mercredi 6 mai 2015

If else OR statement in Assembly?

Confused on using branching commands in Assembly, mostly just BNE, and BEQ. So I have to write an assembly code for the pseudo-code:

X = 5, Y = 10, Z = 15

if X != 4 && Y == 10 || Z = 20

A = X + Y - 2

else R = Z - 5 + X

I'm coding in Keil uVision5 for a ARM Cortex M0 Plus (No clue if this is needed info) within it's low registers which are R0-R7 for this specific board.

I know how I'd write the if else statement itself but I'm mainly asking how I'd go about with the OR part of the code. Obvious X!=4 is true, and Y==10 would be true, and then check if Z = 20 which is false, so would I check if X!=4 first, jump using a BNE label to check Y, use a BEQ to check Z, which is false, but since the equation is true already I'd jump to an BNE endif statement for A=X+Y-2?

and then in between with no label in front of it I'd write the else statement for the assembler to go to if the first BNE label was somehow false..?

A EQU 0x2000000
R EQU 0x2000004
X EQU 0x2000008
Y EQU 0x200000C
Z EQU 0x2000010

 LDR R7, =X
 LDR R0, [R7]
 ADD R0, #5  ; X = 5 in R0
 CMP R0, #4  ; Compare X != 4
 BNE jumpToY

jumpToY
 LDR R7, =Y
 LDR R1, [R7]   ; Y = 10 in R1
 CMP R1, #10    ; Compare Y == 10
 BEQ jumpToZ

jumpToZ
 etc...

Aucun commentaire:

Enregistrer un commentaire