MIPS组装计算器

时间:2016-10-04 06:07:57

标签: assembly mips

嗨,请原谅我,如果我的帖子不符合StackOverflow的标准,因为我是新手。我正在尝试用MIPS汇编语言制作计算器。我的问题是,在运行程序后输入整数值后,答案就会打印出来作为地址。关于为什么我无法获得印刷价值的想法?

这是我的代码:

.data
getnum1: .asciiz "Enter your first number: "
getnum2: .asciiz "Enter your second number: "
getoperator: .asciiz "What type of calculation? (0 - add, 1 - subtract, 2 - multiply): "
result: .asciiz "The result is: "
num1: .word 1       #int 1
num2: .word 1       #int 2
operator: .word 1   #operator
answer: .word 1     #result

.text

main:   
li $v0, 4       #print string
la $a0, getnum1 #"Enter your first number: "
syscall

li $v0, 5       #read int
syscall         
la $s1, num1    #load num1 into s1
sw $v0, num1        #store value into s1 (num1) 

li $v0, 4       #print string
la $a0, getnum2 #"Enter your second number: "
syscall

li $v0, 5       #read int
syscall
la $s2, num2    #load num2 into s2
sw $v0, num2    #store value into s2 (num2)

li $v0, 4       #print string
la $a0, getoperator #"What type of calculation?..."
syscall

li $v0, 5       #read int
syscall
la $s3, operator #load operator into s3
sw $v0, operator #store value into s3 (operator)

la $s0, answer

beq $s3, 0, addition    #if operator is == 0, jump to addition
beq $s3, 1, subtract    #if operator is == 1, jump to subtract
beq $s3, 2, multiply    #if operator is == 2, jump to multiply

li $v0, 4       #print string
la $a0, result  #"The result is: "
syscall

li $v0, 1       #print int
la $a0, answer
syscall

addition:
add $s0, $s1, $s2  #add s1 and s2, store in s0
jr $ra  #return to last spot
subtract:
sub $s0, $s1, $s2  #subtract s1 and s2, store in s0
jr $ra  #return to last spot
multiply:
mult $s1, $s2 #multiply s1 and s2, store in s0
mflo $s0
jr $ra  #return to last spot

0 个答案:

没有答案