2013-02-23 254 views
-1

我正在嘗試編寫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 
+1

什麼是你的問題?你在哪個部分遇到麻煩? – 2013-02-23 00:23:39

回答

1

你的程序是非常接近 - 你只是有幾個問題:

  1. 您錯過了.text指令。這可能應該在newline行之後,並且在main:之前。

  2. 你有一個$ao你可能想要一個$a0接近程序結束。您li $v0,4 #system call code for print string行之後

    la $a0,str3 #address of str3 
    syscall 
    

    :你需要添加 -

  3. 你沒有以往打印str3

  4. 你應該確保在你的程序的末尾添加exit系統調用:

    li $v0,10 
    syscall 
    
+0

非常感謝你對我有幫助。 – user2101075 2013-02-23 01:51:58

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