Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # MIPS Assembly Calculator
- # $s0 = first number
- # $s1 = second number
- # $s2 = operator
- # $s3 = result
- # DATA
- .data
- prompt: .asciiz "> Enter a number: "
- op: .asciiz "> Enter operation (+,-,*,/): "
- nl: .asciiz "\n"
- # CODE
- .globl main
- .text
- main:
- # LOAD OPS
- li $t1, 0x2B # +
- li $t2, 0x2D # -
- li $t3, 0x2A # *
- li $t4, 0x2F # /
- # FIRST INPUT
- la $a0, prompt # print string prompt
- jal PRINT_STRING
- jal READ_INT
- move $s0, $v0 # save to $s0
- # SECOND INPUT
- la $a0, prompt # print string prompt
- jal PRINT_STRING
- jal READ_INT
- move $s1, $v0 # save to $s1
- # OPERATION CHARACTER
- la $a0, op # print string op
- jal PRINT_STRING
- la $v0, 12 # read_char = 12
- syscall
- move $s2, $v0 # save operator to $s2
- la $v0, 12
- syscall # read_char again to get rid of enter key
- # OPERATION BRANCHES
- beq $s2, $t1, opadd
- beq $s2, $t2, opsub
- beq $s2, $t3, opmul
- beq $s2, $t4, opdiv
- j exit
- opadd:
- add $s3, $s0, $s1
- j result
- opsub:
- sub $s3, $s0, $s1
- j result
- opmul:
- mult $s0, $s1
- mflo $s3 # load result from LO
- j result
- opdiv:
- div $s0, $s1
- mflo $s3 # load result from LO, HI contains remainder
- j result
- result: # result is saved in $t2
- move $a0, $s3 # load result to $a0
- la $v0, 1 # print_int = 1
- syscall
- jal NEWLINE
- # EXIT
- exit:
- li $v0, 10
- syscall
- # Routine: Print out a new line
- NEWLINE:
- la $a0, nl # print string nl
- la $v0, 4
- syscall
- jr $ra # return from method
- # Routines: commonly used syscall routines
- PRINT_STRING:
- la $v0, 4
- syscall
- jr $ra
- READ_INT:
- la $v0, 5
- syscall
- jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement