问题
Followup to this
I figured out what went wrong in the linked post. The Sigma0 and Sigma1 boxes weren't doing what they should: modulo 2 addition of the rotated versions of either A or E. Basically, it should work like this:
Notice that if the number of bits in a position is even the result is 0, if odd it's 1. To do this I made a truth table and karnaugh maps:
LINK
I simplified the resulting expression to
!A!BC + A(B XNOR C)
And I tried to apply it to Sigma0 and Sigma1. But it's not working.
For Sigma1, the rotated values and their modulo 2 sum should be as follows:
01010011011101100111101111111101
11101010100110111011001111011111
11001111011111111010101001101110
--------------------------------
01110110100100100110001001001100
But the result is
01010110000100100110001001001100
Comparing the value I got on paper with this one:
01110110100100100110001001001100
01010110000100100110001001001100
Close... Can anyone help me figure out what went wrong?
My code
.data
A: .word 0x87564C0C
B: .word 0xF1369725
C: .word 0x82E6D493
D: .word 0x63A6B509
E: .word 0xDD9EFF54
F: .word 0xE07C2655
G: .word 0xA41F32E7
H: .word 0xC7D25631
W: .word 0x6534EA14
K: .word 0xC67178F2
.text
.globl main
main:
li $s0,0 #loop counter
li $s1,64 #loop limit
main_loop:
bge $s0,$s1,end_main_loop
jal box_0
move $a0,$v0 #save return value in $a0 to be used as argument by box_1
jal box_1
move $a0,$v0 #
jal box_2
move $a0,$v0 #
move $s2,$a0 #will be necessary for the input of box_4 later
jal box_3
move $s3,$v0 #Will be assigned to E later
jal box_4
move $a0,$v0 #
jal box_5
move $s4,$v0 #Will be assigned to A later
###Assignments
lw $a0,G
la $a1,H
sw $a0,($a1) #Old G goes into new H
lw $a0,F
la $a1,G
sw $a0,($a1) #Old F goes into new G
lw $a0,E
la $a1,F
sw $a0,($a1) #Old E goes into new F
#
la $a1,E
sw $s3,($a1) #Output of box_3 goes into new E
#
lw $a0,C
la $a1,D
sw $a0,($a1) #Old C goes into new D
lw $a0,B
la $a1,C
sw $a0,($a1) #Old B goes into new C
lw $a0,A
la $a1,B
sw $a0,($a1) #Old A goes into new B
#
la $a0,A
sw $s4,($a0) #Output of box_5 goes into new A
addi $s0,$s0,1 #increment loop counter
#j main_loop
end_main_loop:
li $v0, 10 # terminate program
syscall
.text
.globl red_boxes
red_boxes:
box_0:
lw $t0,W
lw $t1,K
addu $t0,$t0,$t1 #Wt + Kt
move $v0,$t0
jr $ra
box_1:
addi $sp, $sp, -4
sw $ra, ($sp)
jal Ch
move $t1,$v0
move $t0,$a0 #output of box_0
lw $t3,H
addu $t0,$t0,$t1
addu $t3,$t0,$t3
move $v0,$t3
lw $ra, ($sp)
addi $sp, $sp, 4
jr $ra
box_2:
addi $sp, $sp, -4
sw $ra, ($sp)
jal Sigma1
move $t1,$v0
move $t0,$a0 #output of box_1
addu $t0,$t0,$t1
move $v0,$t0
lw $ra, ($sp)
addi $sp, $sp, 4
jr $ra
box_3:
move $t0,$a0 #output of box_2
lw $t1,D
addu $t0,$t0,$t1
move $v0,$t0
jr $ra
box_4:
addi $sp, $sp, -4
sw $ra, ($sp)
jal Ma
move $t1,$v0
move $t0,$a0 #output of box_2
addu $t0,$t0,$t1
move $v0,$t0
lw $ra, ($sp)
addi $sp, $sp, 4
jr $ra
box_5:
addi $sp, $sp, -4
sw $ra, ($sp)
jal Sigma0
move $t1,$v0
move $t0,$a0 #output of box_4
addu $t0,$t0,$t1
move $v0,$t0
lw $ra, ($sp)
addi $sp, $sp, 4
jr $ra
.text
.globl op_boxes
op_boxes:
Ch:
# (G&!E) || (F&E)
lw $t0,E
lw $t1,F
lw $t2,G
and $t1,$t1,$t0 #(F&E)
not $t0,$t0 #!E
and $t2,$t2,$t0 #(G&!E)
or $t0,$t1,$t2 #(G&!E) || (F&E)
move $v0,$t0
jr $ra
Sigma1:
lw $t0,E #Sigma1
ror $t1,$t0,6 #rotates E to the right by 6 bits X
ror $t2,$t0,11 # ''' by 11 bits Y
ror $t3,$t0,25 # ''' by 25 bitsZ
#!X!YZ + X( Y XNOR Z)
not $t4,$t1 #!X
not $t5,$t2 #!Y
and $t5,$t5,$t4 #!X!Y
and $t6,$t5,$t3 #(!X!Y)Z
subu $sp,$sp,4 # push !X!YZ onto stack
sw $t6,($sp)
xor $t4,$t2,$t3 #Y XOR Z
not $t4,$t4 #Y XNOR Z
and $t4,$t4,$t1 #X( Y XNOR Z)
lw $t3,($sp) # pop !X!YZ
addu $sp,$sp,4
or $t3,$t3,$t4 #!X!YZ + X(Y XNOR Z)
move $v0,$t3
jr $ra
Ma:
# majority = (A&B) | (B&C)
lw $t0,A
lw $t1,B
lw $t2,C
or $t3, $t0, $t2
and $t1, $t1, $t3
and $v0, $t0, $t2
or $v0, $t1, $v0
jr $ra
Sigma0:
#Same as Sigma1 but shifted by different values
lw $t0,A #Sigma0
ror $t1,$t0,2 #X
ror $t2,$t0,13 #Y
ror $t3,$t0,22 #Z
#!X!YZ + X( Y XNOR Z)
not $t4,$t1 #!X
not $t5,$t2 #!Y
and $t5,$t5,$t4 #!X!Y
and $t6,$t5,$t3 #(!X!Y)Z
subu $sp,$sp,4 # push !X!YZ onto stack
sw $t6,($sp)
xor $t4,$t2,$t3 #Y XOR Z
not $t4,$t4 #Y XNOR Z
and $t4,$t4,$t1 #X( Y XNOR Z)
lw $t3,($sp) # pop !X!YZ
addu $sp,$sp,4
or $t3,$t3,$t4 #!X!YZ + X(Y XNOR Z)
move $v0,$t3
jr $ra
EDIT: I think I found it.... there was a little mistake in the truth table. I need to do this again. If anyone wants to save me a lot of time and trouble and beat me to it, be my guest.
EDIT: Oh man I did it!! To think I spent an entire day messing with this when I only needed to instructions. I feel like such an idiot.
Basically this is done with X xor Y xor Z.
来源:https://stackoverflow.com/questions/62035352/performing-modulo-2-addition-in-mips-32-bit-integers