Search code examples
assemblybitmasklc3

I am trying to scale up a bit mask, but while it works at lower numbers, it falls apart at higher ones


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


Solution

  • 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