MIPS汇编语言

时间:2013-02-23 00:21:31

标签: assembly mips

我正在尝试编写MIPS汇编语言,提示用户输入两个描述系统屏幕两维的数字,以像素表示,然后计算并打印屏幕的像素数。

例如,在c ++中:

int width,height,result;
cout<<"Enter width of the device screen: ";
cin>>width;
cout<<"Enter height of the device screen: ";
cout>>height;
result=width*height;
cout<<"The result of the Iphone 4S in pixel: "<<result;

(这是我第一次写这个MIPS程序集,所以我确定这段代码是错误的。我需要有人帮我纠正下面的代码并向我解释。)

.data
str1: .asciiz "Enter width of the device screen: "
str2: .asciiz "Enter height of the device screen: "
str3: .asciiz "The result of the Iphone 4S in pixel: "
newline: .asciiz "\n"

main:
li $v0,4 #system call code for print string
la $a0,str1 #address of str1
syscall #print str1

#get the first number from the user, put into $s0
li $v0,5 #system call for read input
syscall #read integer into $v0 from console.
move $s0,$v0 #move the number read into $s0

#read input string for str2
li $v0,4  #system call code for print string
la $a0,str2 #address of str2
syscall #print str2

#get the second number from the user, put into $s1
li $v0,5 #system call for read input
syscall #read integer into $v0 from console.
move $s1,$v0 #move the number read into $s0

#do the calculation 
mul $s2,$s0,$s1 # s2 is the register to store $s0 and $s1 from the user input.

#read print string for st3
li $v0,4 #system call code for print string

#print width*height
li $v0,1
move $ao,$s2 #move the result of multiplication into $a0 to print
syscall

2 个答案:

答案 0 :(得分:1)

你的程序非常接近 - 你只有几个问题:

  1. 您错过了.text指令。这可能应该在您的newline行之前和main:之前。

  2. 您有一个$ao,您可能希望在程序结束时附近$a0

  3. 您没有打印str3 - 您需要添加:

    la $a0,str3 #address of str3
    syscall
    

    li $v0,4 #system call code for print string行之后。

  4. 您应该确保在程序结束时添加exit系统调用:

    li $v0,10
    syscall
    

答案 1 :(得分:0)

.data
str1: .asciiz "Enter width of the device screen: "
str2: .asciiz "Enter height of the device screen: "
str3: .asciiz "The result of the Iphone 4S in pixel: "
newline: .asciiz "\n"
         .text 

main:
li $v0,4                       #system call code for print string
la $a0,str1                    #address of str1
syscall                        #print str1

                               #get the first number from the user, put into $s0
li $v0,5 #system call for read input
syscall #read integer into $v0 from console.
move $s0,$v0 #move the number read into $s0

#read input string for str2
li $v0,4  #system call code for print string
la $a0,str2 #address of str2
syscall #print str2

#get the second number from the user, put into $s1
li $v0,5 #system call for read input
syscall #read integer into $v0 from console.
move $s1,$v0 #move the number read into $s0

#do the calculation 
mul $s2,$s0,$s1 # s2 is the register to store $s0 and $s1 from the user input.

#read print string for st3
li $v0,4 #system call code for print string
la $a0, str3
syscall
#print width*height
li $v0,1
move $a0,$s2 #move the result of multiplication into $a0 to print
syscall
#
li $v0, 10
syscall