I'm doing some exam practice questions and the first part involves printing out the hexadecimal number (say xFDO1) as a binary number.
My code prints the opposite and I know I can reverse this order by making another loop and instead of starting with the zero bit start with the n bit and bit-shift n-1 times to get the next bit and so on for all the bits but I am hoping there is a better way!
I also know I can hard code all of the different bits and check those against the number but still not an elegant solution.
Here is my code for reference:
.ORIG x3000
LD R1, binary ;loads number we wanna use
LD R2, maskbit ;starts with 0000 0000 0000 0001
LD R4, counter
loop
AND R3,R3,#0 ;resets R3
AND R3,R1,R2 ;checks if has bit there
BRz else
LD R0, ascii1
BR done
else
LD R0, ascii0
done
OUT
ADD R2,R2,R2 ;shift bit one over
ADD R4,R4,#-1 ;decrement counter
BRzp loop ;loops if counter not negative
HALT
counter .fill #15
maskbit .fill x0001
ascii0 .fill x30
ascii1 .fill x31
binary .fill XAF12
.END
I am hoping to find a better approach.
No need to do all of that shifting to the number.
You can use the properties of signed numbers to do this. When you load the number you want to print immediately check if it was negative. Remember that negative numbers have the sign bit set.
Here is some pseudocode