Get five numbers from the user one at a time and print the d
Get five numbers from the user, one at a time, and print the difference of all the negative numbers in the end.
For example, for a sequence in input equal to: -3, 0, -7, -2, 1 the result should be computed as follows: (-3)-(-7)-(-2)=6
This has to be done in MIPS. I believe it is also known as assembly.
We have to do this by only using branches and jumps but I do not know how.
Solution
.LC0:
.ascii \"%d\\000\"
main:
daddiu $sp,$sp,-80
sd $31,72($sp)
sd $fp,64($sp)
sd $28,56($sp)
move $fp,$sp
lui $28,%hi(%neg(%gp_rel(main)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(main)))
ld $2,%got_disp(__stack_chk_guard)($28)
ld $2,0($2)
sd $2,40($fp)
sw $0,4($fp)
sw $0,8($fp)
.L3:
lw $2,8($fp)
slt $2,$2,5
beq $2,$0,.L2
nop
daddiu $3,$fp,16
lw $2,8($fp)
dsll $2,$2,2
daddu $2,$3,$2
move $5,$2
ld $2,%got_page(.LC0)($28)
daddiu $4,$2,%got_ofst(.LC0)
ld $2,%call16(scanf)($28)
move $25,$2
1: jalr $25
nop
lw $2,8($fp)
addiu $2,$2,1
sw $2,8($fp)
b .L3
nop
.L2:
sw $0,12($fp)
.L6:
lw $2,12($fp)
slt $2,$2,5
beq $2,$0,.L4
nop
lw $2,12($fp)
dsll $2,$2,2
daddiu $3,$fp,48
daddu $2,$3,$2
lw $2,-32($2)
bgez $2,.L5
nop
lw $2,12($fp)
dsll $2,$2,2
daddiu $3,$fp,48
daddu $2,$3,$2
lw $2,-32($2)
lw $3,4($fp)
subu $2,$3,$2
sw $2,4($fp)
.L5:
lw $2,12($fp)
addiu $2,$2,1
sw $2,12($fp)
b .L6
nop
.L4:
lw $2,4($fp)
move $5,$2
ld $2,%got_page(.LC0)($28)
daddiu $4,$2,%got_ofst(.LC0)
ld $2,%call16(printf)($28)
move $25,$2
1: jalr $25
nop
move $2,$0
ld $3,%got_disp(__stack_chk_guard)($28)
ld $4,40($fp)
ld $3,0($3)
beq $4,$3,.L8
nop
ld $2,%call16(__stack_chk_fail)($28)
move $25,$2
1: jalr $25
nop
.L8:
move $sp,$fp
ld $31,72($sp)
ld $fp,64($sp)
ld $28,56($sp)
daddiu $sp,$sp,80
j $31
nop


