Bryce
Bryce

Reputation: 97

Shifting a Binary String to the right in machine code/assembly language

I understand that doing 15 shifts to the left would shift a binary sequence to the right by one. However after setting the initial register values, if register 1 is equal to 1; the programs gives the correct solution. Anything larger stored in register 1 causes the program to give the wrong solutions. I'm Working with the LC-3; here is my bin file code:

0011000000000000        ; Orig
0010000011111111        ; R0 <- mem[x3100]
0010001011111111        ; R1 <- mem[x3101]
0101010010100000        ; R2 <- R2 AND #0
0001001001100000        ; R1 <- R1 + #0
0000010000001101        ; BRz R1
0001010010101111        ; R2 <- R2+15
0000010000001001        ; BRz R2
0001000000100000        ; R0 = R0 + #0
0000100000000011        ; BRn
0001000000000000        ; R0 <- R0+R0
0001010010111111        ; R2--
0000111111111010        ; BRnzp PCOffset
0001000000000000        ; R0 <- R0 + R0
0001000000000001        ; R0 <- R0 + #1
0001010010111111        ; R2--
0000111 111110110       ; BRnzp PCOffset
0001001001111111        ; R1--
0000 111 111110001      ; BRnzp PCOffset
0011 000 011101111      ; St R0 -> mem[x3102]
1111000000100101        ; Halt

Upvotes: 1

Views: 629

Answers (1)

Chris M
Chris M

Reputation: 651

Found the line of code that's giving you trouble

0001000000000001        ; R0 <- R0 + #1

this isn't adding 1 to R0 it's actually adding R1 to R0.

0001000000000001        ; R0 <- R0 + R1

What you need to replace that line with is:

0001000000100001        ; R0 <- R0 + #1

You're missing the add immediate bit [5].

Upvotes: 4

Related Questions