~計組~ MIPS 組語寫平方程式

因為最近在考試,就只有先PO上完成的程式碼當參考~

#squre(x)
.text
.globl main
main:
square:
la $t0,word

la $a0,msg #print message for calling for type in
li $v0,4
syscall

li $v0,5 #Read_int
syscall
sw $v0,0($t0)

lw $t2,0($t0)


#不需要用 "li $t1,0" 或 "la $t1,word" 來 initialize $t1
#如果用 "la $t3,word",則運算元內容將會變成 address 而不是 value

Loop:
beq $t1,$t2,EXIT #$t1 is count, $t3 is sum, if i==x, than branch to EXIT
add $t3,$t3,$t2 #j=j+x
addi $t1,$t1,1 #increment i
sw $t3,4($t0)

j Loop
EXIT:

la $a0,ans #print message of result prompt
li $v0,4
syscall

lw $t4,4($t0) #print the answer
li $v0,1
move $a0,$t4
syscall



.data
msg: .asciiz "請輸入一個數字:"
word: .word 0,0
ans: .asciiz "平方的結果是:"


#Finished by Veck Hsiao

System Service:
Set $v0
print_int 1
print_float 2
print_double 3
print_string 4

read_int 5
read_float 6
read_double 7
read_string 8

sbrk 9
exit 10

用法:
li $v0,(immediate) #這裡是設定所要執行的 System call
syscall #這一步就會執行對應的 System call