In: Computer Science
MIPS ASSEMBLY LANGUAGE (I'm using MARS)
Can you provide me with the code of this, without using DIV
(4a-b)%[(2+c)/d] (a,b,c,d are user inputs)
Given, Asked to write a MIPS assembly code to convert the equation without using division operator.
Explanation: As asked in question not to use division operator I used substraction logic to divide the equation then get the equation part by part. Please check the below code for exact functionality.
MIPS Code:
divide:
addiu $sp,$sp,-24
sw $fp,20($sp)
move $fp,$sp
sw $4,24($fp)
sw $5,28($fp)
lw $2,24($fp)
nop
srl $2,$2,31
andi $3,$2,0x00ff
lw $2,28($fp)
nop
srl $2,$2,31
andi $2,$2,0x00ff
xor $2,$3,$2
andi $2,$2,0x00ff
beq $2,$0,$L2
nop
li $2,-1 # 0xffffffffffffffff
b $L3
nop
$L2:
li $2,1 # 0x1
$L3:
sw $2,12($fp)
lw $2,24($fp)
nop
bgez $2,$L4
nop
subu $2,$0,$2
$L4:
sw $2,24($fp)
lw $2,28($fp)
nop
bgez $2,$L5
nop
subu $2,$0,$2
$L5:
sw $2,28($fp)
sw $0,8($fp)
b $L6
nop
$L7:
lw $3,24($fp)
lw $2,28($fp)
nop
subu $2,$3,$2
sw $2,24($fp)
lw $2,8($fp)
nop
addiu $2,$2,1
sw $2,8($fp)
$L6:
lw $3,24($fp)
lw $2,28($fp)
nop
slt $2,$3,$2
beq $2,$0,$L7
nop
lw $3,12($fp)
lw $2,8($fp)
nop
mult $3,$2
mflo $2
move $sp,$fp
lw $fp,20($sp)
addiu $sp,$sp,24
j $31
nop
$LC0:
.ascii "%0.2f\000"
main:
addiu $sp,$sp,-64
sw $31,60($sp)
sw $fp,56($sp)
move $fp,$sp
li $2,7 # 0x7
sw $2,24($fp)
li $2,3 # 0x3
sw $2,28($fp)
li $2,6 # 0x6
sw $2,32($fp)
li $2,2 # 0x2
sw $2,36($fp)
lw $2,24($fp)
nop
sll $3,$2,2
lw $2,28($fp)
nop
subu $2,$3,$2
mtc1 $2,$f0
nop
cvt.s.w $f0,$f0
swc1 $f0,40($fp)
lw $2,32($fp)
nop
addiu $2,$2,2
lw $5,36($fp)
move $4,$2
jal divide
nop
mtc1 $2,$f0
nop
cvt.s.w $f0,$f0
swc1 $f0,44($fp)
lwc1 $f0,40($fp)
nop
cvt.d.s $f0,$f0
lwc1 $f2,44($fp)
nop
cvt.d.s $f2,$f2
mov.d $f14,$f2
mov.d $f12,$f0
jal fmod
nop
cvt.s.d $f0,$f0
swc1 $f0,48($fp)
lwc1 $f0,48($fp)
nop
cvt.d.s $f0,$f0
mfc1 $7,$f0
mfc1 $6,$f1
lui $2,%hi($LC0)
addiu $4,$2,%lo($LC0)
jal printf
nop
move $2,$0
move $sp,$fp
lw $31,60($sp)
lw $fp,56($sp)
addiu $sp,$sp,64
j $31
nop
Please let me know in the comments if you need more help in this.