-1

我正在尝试编写 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
4

2 回答 2

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
    
于 2013-02-23T01:36:05.740 回答
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
于 2014-12-07T12:07:56.850 回答