In this procedure we are deleting the given input string (str) from another string(list). We first check if the str occurs in list. If it occurs we take char with beginning end of str and till the null char than copy them to the (current location – length of str).If the function code is 1 repeat that untill str not found in list.
str1: "r O"
list: "Computer Organization"
function: 1
Output: Computerganization
code:
# Mustafa KOYUNOGLU, 120070034
#
.data 0x10000000
str1: .asciiz "r O"
list: .asciiz "Cdg"
.text
.globl main
main:
la $a0,list
la $a1,str1
addi $a2,$zero,1
jal delete
j exit
delete:
subi $sp,$sp,12 # store the parameters
sw $a0,8($sp)
sw $a1,4($sp)
sw $a2,0($sp)
lb $t0,0($a0) # move parameters to temp registers
lb $t1,0($a1)
move $t2,$a2
addi $t7,$zero,0
loop:
move $t4,$a0 # storre parameters to reuse
move $t5,$a1
lp1:
bne $t0,$t1,notfound # if chars are not same look next
addi $a1,$a1,1 #
addi $a0,$a0,1
lb $t0,0($a0)
lb $t1,0($a1)
bne $t1,$zero,lp1
move $t4,$a0
notfound:
move $a0,$t4
beq $t2,$zero,atla
move $a1,$t5
lb $t1,0($a1)
atla:
subi $sp,$sp,1 #stack e at
lb $t8,0($a0)
sb $t8,0($sp)
addi $t7,$t7,1
addi $a0,$a0,1
lb $t0,0($a0) # sonraki degere bak
bne $t0,$zero,loop
end_of_loop:
lp2:
lb $t6,0($sp) # stringi stackten geri al
sb $t6,0($a0)
addi,$sp,$sp,1
subi $a0,$a0,1
subi $t7,$t7,1
bne $t7,$zero,lp2
lw $a2,0($sp)
lw $a1,4($sp)
addi $a0,$a0,1
# lw $a0,8($sp) address of string to print
addi $sp,$sp,12
li $v0, 4 # system call for print_str
syscall
jr $ra
exit:
#
.data 0x10000000
str1: .asciiz "r O"
list: .asciiz "Cdg"
.text
.globl main
main:
la $a0,list
la $a1,str1
addi $a2,$zero,1
jal delete
j exit
delete:
subi $sp,$sp,12 # store the parameters
sw $a0,8($sp)
sw $a1,4($sp)
sw $a2,0($sp)
lb $t0,0($a0) # move parameters to temp registers
lb $t1,0($a1)
move $t2,$a2
addi $t7,$zero,0
loop:
move $t4,$a0 # storre parameters to reuse
move $t5,$a1
lp1:
bne $t0,$t1,notfound # if chars are not same look next
addi $a1,$a1,1 #
addi $a0,$a0,1
lb $t0,0($a0)
lb $t1,0($a1)
bne $t1,$zero,lp1
move $t4,$a0
notfound:
move $a0,$t4
beq $t2,$zero,atla
move $a1,$t5
lb $t1,0($a1)
atla:
subi $sp,$sp,1 #stack e at
lb $t8,0($a0)
sb $t8,0($sp)
addi $t7,$t7,1
addi $a0,$a0,1
lb $t0,0($a0) # sonraki degere bak
bne $t0,$zero,loop
end_of_loop:
lp2:
lb $t6,0($sp) # stringi stackten geri al
sb $t6,0($a0)
addi,$sp,$sp,1
subi $a0,$a0,1
subi $t7,$t7,1
bne $t7,$zero,lp2
lw $a2,0($sp)
lw $a1,4($sp)
addi $a0,$a0,1
# lw $a0,8($sp) address of string to print
addi $sp,$sp,12
li $v0, 4 # system call for print_str
syscall
jr $ra
exit:
Replace Procedure
In this procedure we replace the given input string (str1) with given another string(str2) in( list). The first part is same with delete procedure. We delete str1 from list then copy the str2 to that place. As we did in delete we shift right byte array considering (str2)’s length. If the function code is 1 repeat that untill str1 not found in list.
str1: "x"
str2: "Mustafa"
function: 1
list: "benim adim x; soyadim x degil"
Output: “benim adim Mustafa; soyadim Mustafa degil”
code:
# Mustafa KOYUNOGLU, 120070034
#
.data 0x10000000
str1: .asciiz "xy"
str2: .asciiz "Mustafa"
function: .byte 48 # unused
list: .asciiz "benim adim x; soyadim x degil"
.text
.globl main
main:
la $a0,list
la $a1,str1
la $a2,str2
addi $a3,$zero,1 # function code
jal replace
j exit
replace:
subi $sp,$sp,16 # store the registers
sw $a0,12($sp)
sw $a1,8($sp)
sw $a2,4($sp)
sw $a3,0($sp)
lb $t0,0($a0) # move values to temp registers
lb $t1,0($a1)
lb $t2,0($a2)
add $t3,$t3,$a3
addi $t7,$zero,0 # move zero to $t7
loop:
move $t4,$a0 # $a0' yu sakla
move $t5,$a1 # $a1' i sakla
move $t6,$a2 # $a2' yi sakla
lp1:
bne $t0,$t1,notfound # eslesme yoksa es gec
addi $a1,$a1,1
addi $a0,$a0,1
lb $t0,0($a0)
lb $t1,0($a1) # sonraki degere bak
bne $t1,$zero,lp1 # stringin sonuna kadar git
move $t4,$a0
lp3:
subi $sp,$sp,1 # stringi stack'e atiyoruz
lb $t8,0($a2)
sb $t8,0($sp)
addi $t7,$t7,1
addi $a2,$a2,1
lb $t2,0($a2)
bne $t2,$zero,lp3
notfound:
move $a0,$t4
beq $t3,$zero,atla
move $a1,$t5
move $a2,$t6
lb $t1,0($a1)
atla:
subi $sp,$sp,1 #stringi stack ten geri alip outputa yaziyoruz
lb $t8,0($a0)
sb $t8,0($sp)
addi $t7,$t7,1
addi $a0,$a0,1 #bir sonraki degere bak
lb $t0,0($a0)
bne $t0,$zero,loop
end_of_loop:
lp2:
lb $t6,0($sp) #
sb $t6,0($a0)
addi,$sp,$sp,1
subi $a0,$a0,1
subi $t7,$t7,1
bne $t7,$zero,lp2
lw $a3,0($sp) #parametreleri ilk haline getir
lw $a2,4($sp)
lw $a1,8($sp)
addi $a0,$a0,1
# lw $a0,12($sp) address of string to print
addi $sp,$sp,16
li $v0, 4 # system call for print_str
syscall
jr $ra # return back
exit:
#
.data 0x10000000
str1: .asciiz "xy"
str2: .asciiz "Mustafa"
function: .byte 48 # unused
list: .asciiz "benim adim x; soyadim x degil"
.text
.globl main
main:
la $a0,list
la $a1,str1
la $a2,str2
addi $a3,$zero,1 # function code
jal replace
j exit
replace:
subi $sp,$sp,16 # store the registers
sw $a0,12($sp)
sw $a1,8($sp)
sw $a2,4($sp)
sw $a3,0($sp)
lb $t0,0($a0) # move values to temp registers
lb $t1,0($a1)
lb $t2,0($a2)
add $t3,$t3,$a3
addi $t7,$zero,0 # move zero to $t7
loop:
move $t4,$a0 # $a0' yu sakla
move $t5,$a1 # $a1' i sakla
move $t6,$a2 # $a2' yi sakla
lp1:
bne $t0,$t1,notfound # eslesme yoksa es gec
addi $a1,$a1,1
addi $a0,$a0,1
lb $t0,0($a0)
lb $t1,0($a1) # sonraki degere bak
bne $t1,$zero,lp1 # stringin sonuna kadar git
move $t4,$a0
lp3:
subi $sp,$sp,1 # stringi stack'e atiyoruz
lb $t8,0($a2)
sb $t8,0($sp)
addi $t7,$t7,1
addi $a2,$a2,1
lb $t2,0($a2)
bne $t2,$zero,lp3
notfound:
move $a0,$t4
beq $t3,$zero,atla
move $a1,$t5
move $a2,$t6
lb $t1,0($a1)
atla:
subi $sp,$sp,1 #stringi stack ten geri alip outputa yaziyoruz
lb $t8,0($a0)
sb $t8,0($sp)
addi $t7,$t7,1
addi $a0,$a0,1 #bir sonraki degere bak
lb $t0,0($a0)
bne $t0,$zero,loop
end_of_loop:
lp2:
lb $t6,0($sp) #
sb $t6,0($a0)
addi,$sp,$sp,1
subi $a0,$a0,1
subi $t7,$t7,1
bne $t7,$zero,lp2
lw $a3,0($sp) #parametreleri ilk haline getir
lw $a2,4($sp)
lw $a1,8($sp)
addi $a0,$a0,1
# lw $a0,12($sp) address of string to print
addi $sp,$sp,16
li $v0, 4 # system call for print_str
syscall
jr $ra # return back
exit:
ConvertCase
In this procedure we convert the all lower letters to upper letters. We check for lower letter byte by byte. When its found add to it’s value 32 and then store it back.
inputList: "cse 338"
outputlist: “CSE 338”
code:
# Mustafa KOYUNOGLU, 120070034
#
.data 0x10000000
inputList: .asciiz "seyita"#
outputList: .asciiz "" #yazdigin inputtan sonra gelecek datayi silmemek cok onemli
.text
.globl main
main:
la $a0,outputList
la $a1,inputList
jal convertcase
j exit
convertcase:
subi $sp,$sp,8 # parametreleri sakla
sw $a0,4($sp)
sw $a1,0($sp)
lb $t0,0($a0) # parametreleri temp register lara at
lb $t1,0($a1)
addi $t2,$zero,122 # kucuk harflerin sonu
addi $t3,$zero,97 # kucuk harflerin ilki
loop:
blt $t1,$t3,pass #kucuk harf degilse es gec
bgt $t1,$t2,pass #kucuk harf degilse es gec
subi $t1,$t1,32 #buyuk harfe cevir
pass:
sb $t1,0($a0)
addi $a1,$a1,1 # sonraki karaktere bak
addi $a0,$a0,1
lb $t1,0($a1)
bne $t1,$zero,loop
end_of_loop:
lw $a1,0($sp)
lw $a0,4($sp) # address of string to print
addi $sp,$sp,16
li $v0, 4 # system call for print_str
syscall
jr $ra
exit:
ConvertNumber
In this procedure we are converting the numbers.For the function 0 and 1 we first check if the value is negative or positive. And then calculate digits’ value. For function 0 digit by digit using or instruction load the number. For function 1 if number is positive add digits relative value to the temp register,if its negative subtract from temp register.
Function:0
İnputlist: 1101
$v0: 0xfffffffd (-3 in decimal)
Function:1
İnputlist: -11
$v0: 0xfffffff5 (-11 in decimal)
Function:2
İnputlist: 3A
$v0: 0x0000003a (58 in decimal)
Function:3
İnputlist: 00111010
outputlist: 3a
code:
# Mustafa KOYUNOGLU, 120070034
#
.data 0x10000001
inputlist: .asciiz "1101"
outputlist: .asciiz ""
.text
.globl main
main:
la $a0,outputlist
la $a1,inputlist
addi $a2,$zero,0
jal convertNumber
j exit
convertNumber:
subi $sp,$sp,12 #store the parameters
sw $a0,8($sp)
sw $a1,4($sp)
sw $a2,0($sp)
li,$t7,1
move $t2,$a2
addi $v0,$zero,0
lb $t4,0($a1) #isareti al
bne $t2,2,gec_hex
addi $t3,$zero,1 #if binary :0, ten :1, hex:
gec_hex:
bne $t2,0,gec
addi $t3,$zero,0 #if binary :0, ten :1, hex:
sb $t3,0($a1) # sadece base 10 da - ve base 2 de 1 gordugunde null yap
gec:
bne $t2,3,gec_3
addi $t3,$zero,0 #if binary :0, ten :1, hex:
gec_3:
bne $t2,1,gec2
lb $t3,0($a1)
bne $t3,45,gec3
addi $t3,$zero,45 #if binary :0, ten :1, hex:
sb $t3,0($a1) # sadece base 10 da - ve base 2 de 1 gordugunde null yap
gec3:
addi $t3,$zero,1 #if binary :0, ten :1, hex:
gec2:
addi $t5,$zero,1
lb $t0,0($a0)
lb $t1,0($a1)
loop_1: # move address of last char to $a1
addi $a1,$a1,1
lb $t1,0($a1)
bne $t1,$zero,loop_1 # sonraki
loop:
subi $a1,$a1,1
lb $t1,0($a1)
beq $t1,$zero,end_of_loop # loop bitti cik
bne $t2,0,look_ten # function code 0 degil 1 mi diye bak
bne $t1,49,pass # bir
sllv $t6,$t5,$t3
add $v0,$v0,$t6
pass:
addi $t3,$t3,1
look_ten:
bne $t2,1,look_hex # function code 1 degil 2 mi diye bak
beq $t1,45,bitti
subi $t5,$t1,48
mul $t6,$t5,$t3
add $v0,$v0,$t6
mul $t3,$t3,10
j look_hex
bitti:
mul $v0,$v0,-1
look_hex:
bne $t2,2,to_print # function code 2 degil 3 mu diye bak
subi $t5,$t1,48
blt $t5,16,cikar
subi $t5,$t5,7
cikar:
mul $t6,$t5,$t3
add $v0,$v0,$t6
mul $t3,$t3,16
to_print:
bne $t2,3,check_loop # if function code is 3 and print result
bne $t1,49,pass_p
sllv $t6,$t5,$t3
add $v0,$v0,$t6
pass_p:
addi $t3,$t3,1
check_loop:
bne $t1,$zero,loop
end_of_loop:
lw $a2,0($sp) # restore parameters back
lw $a1,4($sp)
lw $a0,8($sp)
addi $sp,$sp,12
li $t9, 0
bne $t2,0,pass_1 # do this just for first case
#subi $t3,$t3,1
sllv $t3,$t7,$t3
bne $t4,49,pass_1
sub $v0,$v0,$t3
pass_1:
bne $t2,3,exit1 # do this just for last case
move $t8,$v0
pass_1_1:
subi $t8,$t8,16
bgt $t8,15,pass_1_1
bge $t8,$zero,pass_2
addi $t8,$t8,16
pass_2:
addi $t8,$t8,48
blt $t8,58,ekleme
addi $t8,$t8,7
ekleme:
addi $t9,$t9,1
subi $sp,$sp,1
sb $t8,0($sp)
div $v0,$v0,16
move $t8,$v0
bgt $v0,0,pass_1_1
la $t8,0($a0)
loop_1_2:
lb $t1,0($sp)
addi $sp,$sp,1
sb $t1,0($t8)
addi $t8,$t8,1
subi $t9,$t9,1
bne $t9,$zero,loop_1_2
li $v0, 4
syscall # print result
exit1:
jr $ra
exit:
Sort
We implemented the selection sort algorithm fort his procedure. For function 0 we use exactly selection sort implementation. For function 1 first checking the priorty bytes(numbers and letters),and then sort the rest like function 0. In function 2 we implemented selection sort without swap. Sample for function 2
Function:0
İnputlist: “Cse_338”
outputlist: “338C_es”
code:
# Mustafa KOYUNOGLU, 120070034
#
.data 0x10000000
inputlist: .asciiz "Cse_338"
outputlist: .asciiz ""
.text
.globl main
main:
la $a0,outputlist
la $a1,inputlist
addi $a2,$zero,1
jal sort
j exit
sort:
subi $sp,$sp,12 # parametre leri sakla
sw $a0,8($sp)
sw $a1,4($sp)
sw $a2,0($sp)
lb $t0,0($a0) # parametreleri temp registerlara at
lb $t1,0($a1)
move $t2,$a2
move $t5,$a0 # parametreleri sakla '''''' reuse icin '''''''''''
move $t6,$a1
lb $t7,0($a1) #min ASCII value
li $t4,127
li $t8,0
loop_1:
addi $t8,$t8,1
addi $a1,$a1,1
lb $t1,0($a1)
bne $t1,$zero,loop_1 # string uzunlugunu kaydet
move $a1,$t6
lw $t1,0($a1)
loop:
blt $t1,$t7,pass # kucuk olan degeri al
j pass2
pass:
move $t3,$a1
lb $t7,0($t3)
pass2:
addi $a1,$a1,1
lb $t1,0($a1)
bne $t1,$zero,loop # buraya kadar en kucuk olan sayiyi buluyor
lb $t7,0($t3)
sb $t4,0($t3)
sb $t7,0($t5)
addi $t5,$t5,1
move $a1,$t6
lb $t1,0($a1)
li $t7,127
subi $t8,$t8,1
bne $t8,$zero,loop # buraya geldiginde birini kaydedip bitmis oluyor
end_of_loop:
lw $a2,0($sp) # restore parameters
lw $a1,4($sp)
lw $a0,8($sp)
addi $sp,$sp,12
li $v0,4 # print string
syscall
jr $ra
exit: