In: Computer Science
Solve following tasks using MIPS
Task
Write a program that gets a number from user and
displays “Even”, or “Odd. (If-else)
Write a program that input a number from user displays
each digit separately. For example if input is 3986, output
is 6, 8, 9 ,3. (Loop)
Write a program that converts a decimal number to
binary. (Loop)
Hint for Task 1 and Task 3.
Use, div $tdivident,$tdivider and
mfhi $tdestination instructions.
For example,
Div $t1,$t2 will divide $t1 from $t2 and move the quotient to LOW register and remainder to HI register
mfhi $t3 will move remainder from HIGH register to $t3.
Answer.)
Write a program that gets a number from user and displays “Even”, or “Odd. (If-else)
Solution:
.file 1 "" .section .mdebug.abi32 .previous .nan legacy .module fp=32 .module nooddspreg .abicalls .rdata .align 2 $LC0: .ascii "Enter a number?\000" .align 2 $LC1: .ascii "%d\000" .align 2 $LC2: .ascii "Even\000" .align 2 $LC3: .ascii "Odd\000" .text .align 2 .globl main .set nomips16 .set nomicromips .ent main .type main, @function main: .frame $fp,40,$31 # vars= 8, regs= 2/0, args= 16, gp= 8 .mask 0xc0000000,-4 .fmask 0x00000000,0 .set noreorder .cpload $25 .set nomacro addiu $sp,$sp,-40 sw $31,36($sp) sw $fp,32($sp) move $fp,$sp .cprestore 16 movz $31,$31,$0 lw $2,%got($LC0)($28) nop addiu $4,$2,%lo($LC0) lw $2,%call16(printf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,printf 1: jalr $25 nop lw $28,16($fp) addiu $2,$fp,24 move $5,$2 lw $2,%got($LC1)($28) nop addiu $4,$2,%lo($LC1) lw $2,%call16(__isoc99_scanf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,__isoc99_scanf 1: jalr $25 nop lw $28,16($fp) lw $2,24($fp) nop andi $2,$2,0x1 bne $2,$0,$L2 nop lw $2,%got($LC2)($28) nop addiu $4,$2,%lo($LC2) lw $2,%call16(printf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,printf 1: jalr $25 nop lw $28,16($fp) b $L3 nop $L2: lw $2,%got($LC3)($28) nop addiu $4,$2,%lo($LC3) lw $2,%call16(printf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,printf 1: jalr $25 nop lw $28,16($fp) $L3: move $2,$0 move $sp,$fp lw $31,36($sp) lw $fp,32($sp) addiu $sp,$sp,40 j $31 nop .set macro .set reorder .end main .size main, .-main .ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609"