In: Computer Science
Assume a user wants to classify numbers in terms of whether they
are “divisible by
2”, “divisible 3”, “both visible by 2 and 3” and “neither divisible
by 2 nor 3”. Clearly
for each integer, only one of the classes is most accurate.
Write a SPIM program which allows a user to enter 5 integers and
for each of them,
determines which of the mentioned classes it belongs to.
.LC0:
.ascii "Enter number\000"
.LC1:
.ascii "%d\000"
.LC2:
.ascii "Divisible by 2 only\000"
.LC3:
.ascii "Divisible by 3 only\000"
.LC4:
.ascii "Divisible by 2 & 3 only\000"
.LC5:
.ascii "Not Divisible by 2 and 3\000"
main:
daddiu $sp,$sp,-48
sd $31,40($sp)
sd $fp,32($sp)
sd $28,24($sp)
move $fp,$sp
lui $28,%hi(%neg(%gp_rel(main)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(main)))
li $2,5 # 0x5
sw $2,0($fp)
b .L2
nop
.L7:
ld $2,%got_page(.LC0)($28)
daddiu $4,$2,%got_ofst(.LC0)
ld $2,%call16(puts)($28)
move $25,$2
1: jalr $25
nop
daddiu $2,$fp,4
move $5,$2
ld $2,%got_page(.LC1)($28)
daddiu $4,$2,%got_ofst(.LC1)
ld $2,%call16(__isoc99_scanf)($28)
move $25,$2
1: jalr $25
nop
lw $2,4($fp)
andi $2,$2,0x1
bne $2,$0,.L3
nop
lw $2,4($fp)
move $4,$2
move $3,$4
move $2,$3
dsll $2,$2,2
daddu $2,$2,$3
dsll $5,$2,4
daddu $2,$2,$5
dsll $5,$2,8
daddu $2,$2,$5
dsll $5,$2,16
daddu $2,$2,$5
daddu $2,$2,$3
dsrl $2,$2,32
sll $3,$2,0
sra $2,$4,31
subu $3,$3,$2
move $2,$3
sll $2,$2,1
addu $2,$2,$3
subu $2,$4,$2
beq $2,$0,.L3
nop
ld $2,%got_page(.LC2)($28)
daddiu $4,$2,%got_ofst(.LC2)
ld $2,%call16(puts)($28)
move $25,$2
1: jalr $25
nop
b .L2
nop
.L3:
lw $2,4($fp)
move $4,$2
move $3,$4
move $2,$3
dsll $2,$2,2
daddu $2,$2,$3
dsll $5,$2,4
daddu $2,$2,$5
dsll $5,$2,8
daddu $2,$2,$5
dsll $5,$2,16
daddu $2,$2,$5
daddu $2,$2,$3
dsrl $2,$2,32
sll $3,$2,0
sra $2,$4,31
subu $3,$3,$2
move $2,$3
sll $2,$2,1
addu $2,$2,$3
subu $2,$4,$2
bne $2,$0,.L5
nop
lw $2,4($fp)
andi $2,$2,0x1
beq $2,$0,.L5
nop
ld $2,%got_page(.LC3)($28)
daddiu $4,$2,%got_ofst(.LC3)
ld $2,%call16(puts)($28)
move $25,$2
1: jalr $25
nop
b .L2
nop
.L5:
lw $2,4($fp)
move $4,$2
move $3,$4
move $2,$3
dsll $2,$2,2
daddu $2,$2,$3
dsll $5,$2,4
daddu $2,$2,$5
dsll $5,$2,8
daddu $2,$2,$5
dsll $5,$2,16
daddu $2,$2,$5
daddu $2,$2,$3
dsrl $2,$2,32
sll $3,$2,0
sra $2,$4,31
subu $3,$3,$2
move $2,$3
sll $2,$2,1
addu $2,$2,$3
subu $2,$4,$2
bne $2,$0,.L6
nop
lw $2,4($fp)
andi $2,$2,0x1
bne $2,$0,.L6
nop
ld $2,%got_page(.LC4)($28)
daddiu $4,$2,%got_ofst(.LC4)
ld $2,%call16(puts)($28)
move $25,$2
1: jalr $25
nop
b .L2
nop
.L6:
ld $2,%got_page(.LC5)($28)
daddiu $4,$2,%got_ofst(.LC5)
ld $2,%call16(puts)($28)
move $25,$2
1: jalr $25
nop
.L2:
lw $2,0($fp)
addiu $3,$2,-1
sw $3,0($fp)
bne $2,$0,.L7
nop
move $2,$0
move $sp,$fp
ld $31,40($sp)
ld $fp,32($sp)
ld $28,24($sp)
daddiu $sp,$sp,48
j $31
nop