2014年1月21日 星期二

MIPS programing : separate a word

write a MIPS assembly probram to receive a string as the input in the console

window and output each separate word reversely line by line on the condole window

input: 

computer science is interesting

output: 

ertupmmoc

ecneics

si

gnitseretni


.text
.globl main
main:

li   $t0, 0                #$t0 = i , i=0
li   $t1, 0                #$t1 = j , j=0
lbu  $t3, spa_str($zero)   #$t3 = " "
lbu  $t4, ch_row($zero)    #$t4 = "\n"

li   $v0, 8                #讀字串
la   $a0, arr
syscall

loop1:

lbu  $t2, arr($t0)         #$t2 = value of arr[i]

beq  $t2, $t3, label1      #if(arr[i] == " ") goto label1
beq  $t2, $t4, label1      #else if(arr[i] == '\n') goto label1
addi $t0, $t0, 1           #else i++
j loop1

label1:
add  $s1, $zero, $t0       #$s1 = len , len = i
addi $s1, $s1, -1          #len--

addi $t5, $t1, -1          #$t5 = value of j - 1

loop2:                     #while(len != j-1)  

beq  $s1, $t5, label2      #if(len == j-1) goto labe2

lbu  $t2, arr($s1)         #$t2 = value of arr[len]
sb   $t2, c($zero)         #else c[0] = arr[len]

la   $a0, c                #printf c[0]     
li   $v0, 4 
syscall

addi $s1, $s1, -1          #len--
j loop2

label2:

la   $a0, ch_row           #printf \n
li   $v0, 4
syscall

lbu  $t2, arr($t0)         #$t2 = value of arr[i]
beq  $t2, $t4, exit        #if(arr[i] == '\n') 結束

addi $t1, $t0, 1           #j=i+1
addi $t0, $t0, 1           #i++

j    loop1

exit:
jr   $ra

.data
c:    .space 2
arr:  .space 100
spa_str: .asciiz" "
ch_row:  .asciiz"\n"

沒有留言:

張貼留言