0

As of right here, R6 contains an integer I want to print out in binary.

EndLoop


AND R4, R4, #0
ADD R5, R5, R6     ;This moves my INT to R5

AND R1, R1, #0      
AND R2, R2, #0
LD R2, Count       
NOT R2, R2         
ADD R2, R2, 1 ;This is an offset for making sure we only loop 4 times. I changed it to 8 when trying to make the code scale up

WHILELOOP
ADD R3, R1, R2   ;This checks the loop completion

BRz LOOP_END        


LEA R3, Mask      ;loads the current mask

ADD R3, R3, R1     

AND R4, R4, #0
LDR R4, R3, #0    

AND R4, R4, R5     

BRz NO ;Prints out the 0
LD R0, One
OUT                
ADD R1, R1, #1     
BRnzp WHILELOOP    

NO ;Prints out the 1
LD R0, Zero
OUT                 

ADD R1, R1, #1      
BRnzp WHILELOOP    

LOOP_END

LD R0, Newline
OUT                 
HALT                


Mask  
;   .FILL   b0100000000000000 ;This is what I tried to add
;   .FILL   b0010000000000000
;   .FILL   b0001000000000000
;   .FILL   b0000100000000000
;   .FILL   b0000010000000000
;   .FILL   b0000001000000000
;   .FILL   b000000010000000
;   .FILL   b000000001000000
;   .FILL   b000000000100000
;   .FILL   b000000000010000
 .FILL   b0000000000001000
 .FILL   b0000000000000100
 .FILL   b0000000000000010
 .FILL   b0000000000000001
 .FILL   b0000000000000000

Newline       .FILL   x000A
Zero  .FILL   x0030
One   .FILL   x0031
Count  .FILL   x4     ;I changed this to 14 (because 15 masks)
GREETING      .STRINGZ "Enter a decimal number:"

NEWLINEVAL .FILL -10 ; '\n' is 10

.END

Alright, so this code WORKS. BUT, it only works for 4 bit numbers (0-15). I want it to work for 15 bit numbers, so I tried to add in the Mask you see commented out and make the changes I described in a few of the comments.

But, it doesn't work, and for the life of me I can't figure out why. Maybe I am missing something very stupid. I hope this makes sense, please ask if it doesn't.

Thanks.

Just in case anyone was wondering, the closest I have gotten is with ADD R2, R2, 8 The 15 masks, and count at 14. Putting 15 in for example yields: 0000010001111 Which is pretty close.

Edit as per comments:

Loop 
GETC
OUT     

ADD R5,R4,R0    ;This checks for a return
BRz EndLoop 


ADD R6, R6, R3 ;This does the math
ADD R6, R6, R3
ADD R6, R6, R3
ADD R6, R6, R3
ADD R6, R6, R3
ADD R6, R6, R3
ADD R6, R6, R3
ADD R6, R6, R3
ADD R6, R6, R3

ADD R6, R6, R0
ADD R3, R6, #0
BRnzp Loop  

EndLoop

Luke
  • 483
  • 2
  • 6
  • 13
  • Changing `ADD R2, R2, 1` doesn't make any sense to me, that's just part of the negation of `Count`. Should stay `1`. `Count` should of course be `15`. Also, having a zero in the masks is pointless too. – Jester Mar 01 '16 at 15:17
  • The R2, R2 is used to set the count to be equal to the number of times the loop has run. Count is at 14 because it starts at 0, and you are right, the extra 0 in the masks could be removed. Just in case, I did run test all of those ideas, and it made things worse. Thanks though. – Luke Mar 01 '16 at 15:22
  • The `NOT` and the `ADD` form a two's complement negation so `R2` is set to `-Count`. That makes `R1 + R2` equal zero when `R1 == Count` and hence exit the loop. – Jester Mar 01 '16 at 15:30
  • Sorry, I have been awake for about 40 hours now. I know what the NOT and the ADD do, but the offset needs to be scaled up as the count increases, right? Setting it to 8 results in closer results. – Luke Mar 01 '16 at 15:34
  • 1
    No, two's complement negation is always `NOT + 1`. That code is just doing `R2 = -Count`. No change needed there. – Jester Mar 01 '16 at 15:36
  • Must be a coincidence then, I've just been fiddling for a while just seeing what happens. I'm really stuck, it would almost be better if it didn't work perfectly for 4 bits because then I could better diagnose the problem. – Luke Mar 01 '16 at 15:43
  • Also your mask is missing a value. – Jester Mar 01 '16 at 15:47
  • Whoops, sadly no overall change. I think that may have been a typo in copying to stack. – Luke Mar 01 '16 at 15:58

1 Answers1

1

This works fine here. I had to change to hex constants as my version of lc3 apparently does not support binary.

LD R6,input

AND R4, R4, #0
ADD R5, R5, R6     ;This moves my INT to R5

AND R1, R1, #0      
AND R2, R2, #0
LD R2, Count       
NOT R2, R2         
ADD R2, R2, 1

WHILELOOP
ADD R3, R1, R2   ;This checks the loop completion

BRz LOOP_END        


LEA R3, Mask      ;loads the current mask

ADD R3, R3, R1     

AND R4, R4, #0
LDR R4, R3, #0    

AND R4, R4, R5     

BRz NO ;Prints out the 0
LD R0, One
OUT                
ADD R1, R1, #1     
BRnzp WHILELOOP    

NO ;Prints out the 1
LD R0, Zero
OUT                 

ADD R1, R1, #1      
BRnzp WHILELOOP    

LOOP_END

LD R0, Newline
OUT                 
HALT                


Mask  
 .FILL x4000
 .FILL x2000
 .FILL x1000
 .FILL x0800
 .FILL x0400
 .FILL x0200
 .FILL x0100
 .FILL x0080
 .FILL x0040
 .FILL x0020
 .FILL x0010
 .FILL x0008
 .FILL x0004
 .FILL x0002
 .FILL x0001

Newline       .FILL   x000A
Zero  .FILL   x0030
One   .FILL   x0031
Count  .FILL  15     ;I changed this to 14 (because 15 masks)
GREETING      .STRINGZ "Enter a decimal number:"

NEWLINEVAL .FILL -10 ; '\n' is 10
Input .FILL x4321
.END
Jester
  • 56,577
  • 4
  • 81
  • 125
  • Huh, that's really odd. It would appear the mistake is in my math above that gets the integer from the character string. I didn't think that would be the case because it was working. Hmm, I'm going to have to take a closer look at it, if you are interested I edited it into the OP. The algorithm is pretty simple, it just take the first digit, then muliplies it by 10 and adds the second, multiplies by 10 and adds, the third and so on. – Luke Mar 01 '16 at 16:20
  • You know you have to subtract the ascii code of `0` (which is 48) from each digit, right? Because you don't seem to do that. – Jester Mar 01 '16 at 16:24
  • Oh jeez, you are right. I actually had that in there prior and it must have gotten moved at some point. Working like a charm, thanks for all your help. – Luke Mar 01 '16 at 16:31