2013年6月26日 星期三

MIPS

MIPS組語!!
.data
str1: .asciiz "Please input string with lenght 9 \n"
str2: .asciiz "This string is that you input\n"
.text                        
.align 2
.globl main                    # main為全域變數
main:      
li $v0, 4                        # print_string的系統呼叫程式碼
la  $a0, str1                 # 要列印字串的位置在str1
syscall                           # 印出字串
 li  $v0, 8                       # read_string的系統呼叫程式碼
 li  $a1, 10                     # 可讀入的字串長度9
 syscall                           # string的起始位置為暫存器$a0
 addi $t0, $a0, 0               # 把暫存器$a0拷貝至暫存器$t0
 li  $v0, 4                      # print_string的系統呼叫程式碼
 la   $a0, str2               # 要列印字串的位置在str2
 syscall                           # 印出字串
 li  $v0, 4
 addi $a0, $t0 ,0         # 把暫存器$t0拷貝至暫存器$a0
 syscall

#*****************************2***********************************#
.text                              # text section
.globl main                 # call main by SPIM
main:    
la $t0, value             # load address ‘value’ into $t0
lw $t1, 0($t0)          # load word 0(value) into $t1
lw $t2, 4($t0)          # load word 4(value) into $t2
add $t3, $t1, $t2    # add two numbers into $t3
sw $t3, 8($t0)         # store word $t3 into 8($t0)

 li  $v0,1
 addi $a0, $t3,0        # 把暫存器$t3拷貝至暫存器$a0
 syscall

.data                         
value:    .word 10, 20, 0      # data for addition

#*****************************3***********************************#
 #--01印出0到10整數的2平方和
.text
.align 2                        #// alignment 2^2 bytes
.globl main
main:
 subu $sp, $sp, 32     #// R29 (stack pointer); allocate the frame
 sw $ra, 20($sp)        #// R31; push the return address
 sw $0,  24($sp)        #// R0 (constant 0); Sum = 0;
 sw $0,  28($sp)        #// i = 0;
 la $a0, str                 #//$a0指在字串的起始位址"The Sum ..."
 li $v0, 4                    #//顯示字串的系統呼叫碼為4
 syscall                       #//呼叫系統
loop:
 lw $t6, 28($sp)         #// R14 (temporary 6; not pushed); i
 mul $t7, $t6, $t6     #// R15 (temporary 7; not pushed); i*i
 lw $t8, 24($sp)         #// R24 (temporary 8; not pushed); Sum
 addu $t9, $t8, $t7   #// R25 (temporary 9; not pushed); Sum + i*i
 sw $t9, 24($sp)        #// Sum += (i*i);
 addu $t0, $t6, 1       #// R8 (temporary 0; not pushed); i+1
 sw $t0, 28($sp)        #// i++;
 ble $t0, 10, loop       #// if (i<=10) goto loop;
 li $v0,1                       #//顯示數值的系統呼叫碼為1
 move $a0,$t9           #//答案385
 syscall                         #//呼叫系統

.data
.align 0                       #// alignment disabled
str:
.asciiz "The Sum from 0 .. 10 is %d\n"

#*****************************4***********************************#
#--02 印出0到10整數的3平方和
.text
.align 2                        #// alignment 2^2 bytes
.globl main
main:
 subu $sp, $sp, 32      #// R29 (stack pointer); allocate the frame
 sw $ra, 20($sp)         #// R31; push the return address
 sw $0,  24($sp)         #// R0 (constant 0); Sum = 0;
 sw $0,  28($sp)         #// i = 0;
 la $a0, str                   #//$a0指在字串的起始位址"The Sum.."
 li $v0, 4                      #//顯示字串的系統呼叫碼為4
 syscall                         #//呼叫系統
loop:
 lw $t6, 28($sp)         #// R14 (temporary 6; not pushed); i
 mul $t7, $t6, $t6     #// R15 (temporary 7; not pushed); i*i
 mul  $t7, $t7, $t6    #// R15 (temporary 7; not pushed); i2*i
 lw $t8, 24($sp)         #// R24 (temporary 8; not pushed); Sum
 addu $t9, $t8, $t7   #// R25 (temporary 9; not pushed); Sum + i*i

 sw $t9, 24($sp)         #// Sum += (i*i);
 addu $t0, $t6, 1        #// R8 (temporary 0; not pushed); i+1
 sw $t0, 28($sp)         #// i++;
 ble $t0, 10, loop        #// if (i<=10) goto loop;
 li $v0,1                        #//顯示數值的系統呼叫碼為1
 move $a0,$t9            #//答案3025 1+8+27+64+125+216+343+512+729+1000
 syscall   #呼叫系統

.data
.align 0                        #// alignment disabled
str:
.asciiz "The Sum from 0 .. 10 is %d\n"


沒有留言:

張貼留言