计算机四则运算的的代码设计,MIPS汇编程序设计——四则运算计算器(示例代码)...

#https://blog.csdn.net/oncoding/article/details/4346531

#sample example ‘a small calculater’

# data section

.data

value: .word0, 0, 0 ,0 ,0 # 0: first num ,4 : second num , 8 : operation , 12:result

msg0 : .asciiz"please choose the operation(1~4):

1 : +,addition

2 : -,subtracter

3 : * multiplication

4 : /,division"msg1 : .asciiz"first num:"msg2 : .asciiz"second num:"addFlag : .asciiz"+"subFlag : .asciiz"-"mulFlag : .asciiz"*"divFlag : .asciiz"/"equalStr : .asciiz"="newline : .asciiz"==================================".text # text section

.globl main # call main by SPIM

main:

la $t0, value # load address"value"into $t0

la $a0,msg0

li $v0,4syscall #print"please choose the operation:"li $v0,5syscall

sw $v0,8($t0) #input operatorla $a0,msg1

li $v0,4syscall #print"first num:"li $v0,5syscall

sw $v0,0($t0) #read a

la $a0,msg2

li $v0,4syscall #print"second num:"li $v0,5syscall

sw $v0,4($t0) #read b

la $a0,newline

li $v0,4syscall #print""lw $t1,0($t0) # load the first num

lw $t2,4($t0) # load the second num

lw $t3,8($t0) # load the operation

beq $t3,1,addOp # if +beq $t3,2,subOp # if -beq $t3,3,mulOp # if *beq $t3,4,divOp # if /addOp:

add $t4, $t1, $t2 # $t1+ $t2 =$t4

sw $t4,12($t0) #

la $t5,addFlag

j printResult

subOp:

sub $t4, $t1, $t2 # $t1- $t2 =$t4

sw $t4,12($t0)

la $t5,subFlag

j printResult

mulOp:

mul $t4, $t1, $t2 # $t1* $t2 =$t4

sw $t4,12($t0)

la $t5,mulFlag

j printResult

divOp:

div $t4, $t1, $t2 # $t1/ $t2 =$t4

sw $t4,12($t0)

la $t5,divFlag

j printResult

printResult:

lw $a0,0($t0)

li $v0,1syscall #read first number

la $a0,0($t5)

li $v0,4syscall #print opflag

lw $a0,4($t0)

li $v0,1syscall #print second number

la $a0,equalStr

li $v0,4syscall #print"="lw $a0,12($t0)

li $v0,1syscall # print sum result

j exit

exit:

la $a0,newline

li $v0,4syscall #print""#j main

li $v0,10syscall # exit

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值