Skip to content

Instantly share code, notes, and snippets.

@parastuffs
Last active August 29, 2015 13:56
Show Gist options
  • Save parastuffs/8922523 to your computer and use it in GitHub Desktop.
Save parastuffs/8922523 to your computer and use it in GitHub Desktop.
ELECH473 - Lab 1 - Write a program which adds the unsigned content of reg1 and reg2 and writes the result in reg3 and the carry bit in reg4.
nop
//R1 : op 1
//R2 : op 2
//R3 : mask, then final result
//R4 : report flag
//R5 : temp 1, used fo R1 NAND R3, loop verification, temporary serious science, final temp to check flag status
//R6 : temp 2, temp 0xFFFF for serious science
//R7 : temp 3 - loop and 0xFFFF
movi 1,0xEFFF //We want to store a 16-bits word. "addi" is not enough.
movi 2,0xEFFF
addi 3,0,1 //set the mask in R3, initialized at 1
nand 5,1,3 //NOT(R1 && R3) in R5
nand 6,2,3
beq 5,6,flag //If equal, maybe set the flag, iff the result is not 0xFFFF
beq 0,0,loop //If not equal, skip the operation setting the flag (label "flag") and go directly to the loop.
flag: movi 5,0xFFFF //put 0xFFFF inside R5. R5 will act as a buffer (its content does not interest us anymore)
beq 5,6,loop //If R6 (the result of R2 NAND mask, which was equal to R1 NAND mask) == 0xFFFF, it means that at the set bit in the mask (the position of the bit set to 1 in the mask), we had 0 in both operands (stored in R1 and R2). That means that we would have the same NAND mask, but no report needed.
add 4,3,3 //Set the mask at the mask + the mask (mask stored in R3). Flag.
loop: movi 5,0x4000 //Stores the constant to be checked.
beq 3,5,final_op //We want to run the loop 14 times, that is, until the mask is equal to 0100 0000 0000 0000 (0X4000 in hexa). We'll increment the mask at each run and it will eventually end up on 0x4000.
add 3,3,3 //Shift the mask. We want to move the set bit one place to the left, this is done by mutlplying the mask by 2.
nand 5,1,3
nand 6,2,3
beq 5,6,flag_loop
nand 5,4,3 //If not equal proceed to some serious science. Temp(R5) = flag NAND mask
nand 5,5,3 //Temp = temp NAND mask
movi 6,0xFFFF
beq 5,6,flag_loop //If temp == 0xFFFF, set flag.
beq 0,0,loop //If not equal, just ignore the flag and rerun.
flag_loop: movi 5,0xFFFF //May be made more efficient by using R7
beq 5,6,loop
add 4,3,3
beq 0,0,loop
//Final operations
final_op: add 3,3,3 //Update mask
nand 5,1,3
nand 6,2,3
beq 5,6, report
nand 5,4,3 //If not equal proceed to some serious science. Temp = flag NAND mask
nand 5,5,3 //Temp = temp NAND mask
movi 6,0xFFFF
beq 5,6,report //If temp == 0xFFFF, report.
beq 0,0,end
report: movi 5,0xFFFF
beq 5,6,end
addi 4,0,1 //Carry bit.
end: add 3,1,2 //Addition
addi 5,0,1 //Set R5, temp, to check the flag
beq 5,4,halt
addi 4,0,0 //If the msb of the flag array is not set, this means the operation does not need a report => empty the array
beq 0,0,halt
halt: halt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment