码迷,mamicode.com
首页 > 其他好文 > 详细

MIPS汇编程序设计——四则运算计算器

时间:2018-09-18 22:48:34      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:==   odi   blog   coding   ESS   value   简单的   str   col   

实验目的

运用简单的MIPS实现一个能够整数加减乘除的计算器,同时使自己更加熟悉这些指令吧

MIPS代码

#https://blog.csdn.net/oncoding/article/details/4346531
#sample example a small calculater’  
# data section  
.data                 
value:  .word 0, 0, 0 ,0 ,0     # 0: first num ,4 : second num , 8 : operation , 12:result  
msg0        :   .asciiz  " please choose the operation(1~4):\n\t\t1 : +,addition \n\t\t2 : -,subtracter\n\t\t3 : * multiplication \n\t\t4 : /,division\n"  
msg1        :   .asciiz  "first num:"  
msg2        :   .asciiz  "second num:"  
addFlag     :   .asciiz  " + "  
subFlag     :   .asciiz  " - "  
mulFlag     :   .asciiz  " * "  
divFlag     :   .asciiz  " / "  
equalStr    :   .asciiz  " = "  
newline     :   .asciiz  "\n==================================\n"
.text                       # text section  
.globl main                 # call main by SPIM  
main:  
    la $t0, value           # load address "value" into $t0  
    la $a0,msg0  
    li $v0,4          
    syscall              #print "please choose the operation:"  
      
    li  $v0, 5    
    syscall  
    sw  $v0, 8($t0)      #input operator
      
    la $a0,msg1  
    li $v0,4          
    syscall              #print "first num:"  
      
    li  $v0, 5    
    syscall  
    sw  $v0, 0($t0)      #read a
      
    la $a0,msg2  
    li $v0,4          
    syscall              #print " second num:"  
      
      
    li  $v0, 5    
    syscall  
    sw  $v0, 4($t0)      #read b 
      
    la $a0,newline  
    li $v0,4          
    syscall              #print "\n"  
      
    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,1          
    syscall             #read first number  
      
    la $a0,0($t5)  
    li $v0,4   
    syscall             #print opflag  
      
    lw $a0,4($t0)  
    li $v0,1          
    syscall             #print second number  
      
    la $a0,equalStr  
    li $v0,4   
    syscall             #print " = "  
      
    lw $a0,12($t0)  
    li $v0,1          
    syscall             # print sum result  
    j exit  
      
exit:  
    la $a0,newline  
    li $v0,4   
    syscall             #print " \n "  
     
    #j main 
    li $v0,10         
    syscall             # exit  

 

MIPS汇编程序设计——四则运算计算器

标签:==   odi   blog   coding   ESS   value   简单的   str   col   

原文地址:https://www.cnblogs.com/lfri/p/9671231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!