16 Bits ALU in MIPS

86 Views Asked by At

I am trying to solve this question:

Question

I implemented this code in PCSpim:

## Two 16 bits value is assigned in data segment 
## Provide Selector value in console window
## Result is displayed
.text
.globl main

main:
    la $s1, value   
    lb $t1, 0($s1)
    lb $t2, 2($s1)
    lb $t3, 4($s1)
    xor $t7,$t7,$t7
    

    li $v0, 4
    la $a0, msg1
    syscall 
    
    li $v0, 5   ##selector
    syscall
    add $s3, $v0, $0 ##selector in s3
    
    addi $s4, $0, 0  ##carry in 0 in s4


    addi $t0,$0, 3
    bne $t0, $s0, LabelY
    addi $s4, $0, 1  ##carry in 1 substruction
        
LabelY:
    bgt $t3,15,exit1
    addi $t3,$t3,1

    jal aBitALU

        
    li $v0, 1
    move $a0, $t6
    syscall
    
    
    srl $t1,$t1,1
    srl $t2,$t2,1
    j LabelY

    #li  $v0, 10
    #syscall
    
aBitALU:
    add $s0, $0, $s3
    add $s1, $0, $s4
    andi $t5, $t2, 1
    andi $t4, $t1, 1


    addi $t0,$0, 0
    beq $t0, $s0, Label0

    addi $t0,$0, 1
    beq $t0, $s0, Label1

    addi $t0,$0, 2
    beq $t0, $s0, Label2
    addi $t0,$0, 3
    beq $t0, $s0, Label3
    addi $t0,$0, 4
    beq $t0, $s0, Label4
    addi $t0,$0, 5
    beq $t0, $s0, Label5
    addi $t0,$0, 7
    beq $t0, $s0, Label7
    
    

Label0: 
    or $t6, $t5, $t4   #binary or
    jr $ra

Label1: 
    and $t6, $t5, $t4  #binary and
    jr $ra

Label2: 
                    #binary addition

    or $t4,$t4,$t7  
    xor $a0, $t4, $t5 #sum
    xor $a1, $a0, $s1
    
    and $a0, $a0, $s1

    and $a2, $t4, $t5

    or $a0, $a0, $a2 #cout

    add $t6, $a1, $0
    add $t7, $a0, $0

    jr $ra

Label3: 
                    #binary 2's complement substruction

    nor  $t7, $t5, $t5
    andi $t7, $t7,1

    xor $a0, $t4, $t7 #sum
    xor $a1, $a0, $s1
    
    and $a0, $a0, $s1
    and $a2, $t4, $t7

    or $a0, $a0, $a2 #cout

    add $t6, $a0, $0
    add $t7, $a1, $0

    jr $ra

Label4:
    beq $t3,1,l1
    bne $t3,1,l2
    l1:
    sll $t1,$t1,1
    xor $t6,$t6,$t6
    andi $t6, $t1, 1
    jr $ra
    
    l2:
    xor $t6,$t6,$t6
    andi $t6, $t1, 1
    jr $ra
Label5:
    beq $t3,1,l3
    bne $t3,1,l4
    l3:
    srl $t1,$t1,1
    xor $t6,$t6,$t6
    andi $t6, $t1, 1
    jr $ra
    
    l4:
    xor $t6,$t6,$t6
    andi $t6, $t1, 1
    jr $ra
Label7: 
    slt $t6, $t2, $t1    #if t2 less then t1 then t6=1 otherwise t6=0
    j exit
exit:
    li $v0, 1
    move $a0, $t6
    syscall
exit1:
    li $v0, 4
    la $a0, msg2
    syscall
        
    li $v0, 1
    move $a0, $t6
    syscall
    
    li $v0, 1
    move $a0, $t7
    syscall

.data
value: .half 5, 6, 0
msg1:  .asciiz "Selector Value = "

msg2:  .asciiz "Result = "

When I run this, console shows:

console output

Selector value 2 mean add (5+6=11)

In result = 00 I mean first zero flag then carry flag but in output MSB show last and LSB shows in first I mean is the MSB become LSB and LSB become MSB where it should be 0000 0000 0000 1101

How can I solve this?

0

There are 0 best solutions below