I'm trying to implement a code that compares 2 numbers and stores in the register R1 the largest. The following code is doing the job:
.ORIG 0x300
V1: .FILL 0x007
V2: .FILL 0x020
LD R2, V1 ;R2=V1
LD R3, V2 ;R3=V2
AND R4, R4, #0
ADD R4, R4, R2 ;R4=R2
AND R5, R5, #0
ADD R5, R5, R3 ;R5=R3
NOT R5, R5
ADD R5, R5, #1 ;R5=-R4
ADD R4, R4, R5
BRN SECOND_BIGGER
LD R1, V1
JSR END
SECOND_BIGGER:
LD R1, V2
END:
.END
But when I tried to replace the part where I copy the 2 values on the registers R4 and R5 it stopped working:
.ORIG 0x300
V1: .FILL 0x007
V2: .FILL 0x020
LD R2, V1 ;R2=V1
LD R3, V2 ;R3=V2
STR R4, R2, #0
STR R5, R3, #0
NOT R5, R5
ADD R5, R5, #1 ;R5=-R4
ADD R4, R4, R5
BRN SECOND_BIGGER
LD R1, V1
JSR END
SECOND_BIGGER:
LD R1, V2
END:
.END
The code gets executed but displays the wrong result, any idea of the reason?
CodePudding user response:
Use the add with immediate form, for example: ADD R4, R2, #0
. The value in R2
will be added with 0
and placed into R4
. That's your basic register to register copy/move.
Addition of 0 is more work than you need to copy one register to another (e.g. the addition operation is useless) but reusing existing instructions makes the hardware simpler, and even though in theory one that doesn't involve addition could run faster (by forgoing the addition) that likely won't change the overall cycle time, and would require more hardware and possibly occupation of encodings.
The loads and stores are for only for copying data between CPU registers and memory locations.