Is there any problem with my approach to swap element in array string?

0

I'm doing an assignment in MIPS that sort list of students based on his/her score. I have three arrays, 1 to save name of student, 1 to save the address to each student name, the other is to save their score. I've managed to sort array of score using bubble sort but I can't do the same approach with the array that store name. I can swap 2 number but i try to swap 2 address to student name, the error occured I got the error: address out of range 0x00000000 Any idea on how to swap elements in array string ?

All variable

op:         .asciiz "Enter number of student:"
prompt:     .asciiz "Enter name of student:"
promptNum:  .asciiz "Enter score:"
text:       .asciiz "The list of student after sort is:"
newline:    .asciiz "\n"

array:      .space  20      # Store address of name array
numarr:     .space  80      # 20 student * 4 bytes per mark
string:     .space  20000   # Store name of student

Main

main:
    # prompt user for array length
    li          $v0,4
    la          $a0,op
    syscall
    jal         new_line            # output newline

    # read in array count
    li          $v0,5
    syscall
    addi        $s0,$v0,0           # $s0 contains the integer we read

    add         $t0,$zero,$zero     # index of array
    addi        $t1,$zero,1         # counter=1
    la          $s2,string          # load address of string storage area

Read Name

    # prompt the user for next student name
    li          $v0,4
    la          $a0,prompt
    syscall

    # get the string
    move        $a0,$s2             # place to store string 
    li          $a1,20
    li          $v0,8
    syscall

    # store pointer to string into array
    sw          $a0,array($t0)

Read Score

    #prompt the user for next student score
    li      $v0,4
    la      $a0, promptNum
    syscall 

    #get the number 
    li      $v0,5
    syscall

    #get the number 
    move    $a0, $v0

    #store pointer to number into array number
    sw      $a0,numarr($t0)

Incrementing offset

    addi        $t0,$t0,4           # advance offset into pointer array
    addi        $t1,$t1,1           # advance iteration count
    addi        $s2,$s2,20          # advance to next string area

Bubble sort

la  $s7, numarr #load address of numarr to $s7
la  $t6, array  #load adress of arr to $s8
li  $s3, 0      #initialize counter 1 for loop 1
subi    $s6, $s0, 1 #  n -1
li  $s4, 0      # initialize countr 2 for loop 2

sort_int:
    sll $t7, $s4, 2                 #multiply $s4 by 2 and put it in t7
    add $t7, $s7, $t7               #add the address of numbers to t7
    sll $t8, $s4, 2                 #multiply $s4 by 2 and put it in t8
    add $t8, $t6, $t8               #add the address to find current element

    lw $t3, 0($t7)                  #load numbers[j]    
    lw $t4, 4($t7)                  #load numbers[j+1]

    lw $t5, 0($t8)                  #load address to string[j]
    lw $t6, 4($t8)                  #LOAD address string [j+1]

    slt $t2, $t3, $t4               #if t3 < t4
    bne $t2, $zero, increment

    sw $t4, 0($t7)                  #swap number
    sw $t3, 4($t7)

    ####------>>> THIS IS THE ERROR LINE (address out of range 0x00000000)
    sw $t6, 0($t8)                  #swap address of 2 name
    sw $t5, 0($t8)

Print name and score

print:
    bgt         $t1,$s0,done        # more strings to output?  if no, fly

    lw          $t2,array($t0)      # get pointer to string
    # output the string
    li      $v0,4           #4 -> string    
    move        $a0,$t2
    syscall
    #jal        new_line

    lw          $t2,numarr($t0)      #get pointer to number    
    # output the number 
    li      $v0,1            # 1-> number   
    move    $a0, $t2
    syscall
    jal     new_line

    addi        $t0,$t0,4           # advance array index
    addi        $t1,$t1,1           # advance count
    j           print

This is the full code

    .data
    .align 2
array:      .space  80
numarr:     .space  80
string:     .space  20000       
op:         .asciiz "Enter number of student:"
prompt:     .asciiz "Enter name of student:"
promptNum:  .asciiz "Enter score:"
text:       .asciiz "The list of student is:"
newline:    .asciiz "\n"
###########################
#$t0: index 
#$t1: counter
#$s0: contain number of student
#$s2: store string
#
############################# 

       .text
    .globl main
main:
    # prompt user for array length
    li          $v0,4
    la          $a0,op
    syscall
    jal         new_line            # output newline

    # read in array count
    li          $v0,5
    syscall
    addi        $s0,$v0,0           # $s0 contains the integer we read

    add         $t0,$zero,$zero     # index of array
    addi        $t1,$zero,1         # counter=1
    la          $s2,string          # load address of string storage area 

read_string:
    bgt      $t1,$s0,L1          # if ($t1 > length) then array is done -- fly

    # prompt the user for next student name
    li          $v0,4
    la          $a0,prompt
    syscall

    # get the string
    move        $a0,$s2             # place to store string 
    li          $a1,20
    li          $v0,8
    syscall

    # store pointer to string into array
    sw          $a0,array($t0)

    #prompt the user for next student score
    li      $v0,4
    la      $a0, promptNum
    syscall 

    #get the number 
    li      $v0,5
    syscall

    #get the number 
    move    $a0, $v0

    #store pointer to number into array number
    sw      $a0,numarr($t0)

    addi        $t0,$t0,4           # advance offset into pointer array
    addi        $t1,$t1,1           # advance iteration count
    addi        $s2,$s2,20          # advance to next string area [NEW]
    j           read_string

#### here i want to print the array ####
L1:
    add         $t0,$zero,$zero     # index of array
    addi        $t1,$zero,1         # counter = 1

    # output the title
    la          $a0,text
    li          $v0,4
    syscall
    jal         new_line

add         $t0,$zero,$zero     # index of array
addi        $t1,$zero,1         # counter = 1

#s0 length
#$t0 index
#$t1 counter
#s0 -> s3
#s6 -> s6
#s1 - > s4
#s7 -> address of numarr
#t6 -> address of arr
la  $s7, numarr #load address of numarr to $s7
la  $t6, array  #load adress of arr to $s8
li  $s3, 0      #initialize counter 1 for loop 1

subi    $s6, $s0, 1 #  n -1

li  $s4, 0      # initialize countr 2 for loop 2

#s0 length
#$t0 index
#$t1 counter
#s0 -> s3   counter1
#s6 -> s6   s6 = n - 1
#s1 - > s4  counter 2
#s7 -> address of numarr
#t6 -> address of arr 
sort_int:
    sll $t7, $s4, 2                 #multiply $s4 by 2 and put it in t7
    add $t7, $s7, $t7               #add the address of numbers to t7
    sll $t8, $s4, 2                 #multiply $s4 by 2 and put it in t8
    add $t8, $t6, $t8               #add the address to find current element

    lw $t3, 0($t7)                  #load numbers[j]    
    lw $t4, 4($t7)                  #load numbers[j+1]

    lw $t5, 0($t8)                  #load address to string[j]
    lw $t6, 4($t8)                  #LOAD address string [j+1]

    slt $t2, $t3, $t4               #if t3 < t4
    bne $t2, $zero, increment

    sw $t4, 0($t7)                  #swap number
    sw $t3, 4($t7)

    sw $t6, 0($t8)                  #swap address of 2 name
    sw $t5, 0($t8)

#s0 length
#$t0 index
#$t1 counter
#s0 -> s3   counter1
#s6 -> s6   s6 = n - 1
#s1 - > s4  counter 2
#s7 -> address of numarr
#t6 -> address of arr
#$t7 ->
#t3 -> 
# t4 -> 
increment:  

    addi $s4, $s4, 1                #increment t1
    sub $s5, $s6, $s3               #subtract s3 from s6

    bne  $s4, $s5, sort_int             #if s4 (counter for second loop) does not equal 9, loop
    addi $s3, $s3, 1                #otherwise add 1 to s3
    li $s4, 0                   #reset s1 to 0

    bne  $s3, $s6, sort_int             # go back through loop with s1 = s1
#$s0 length
#$t0 index
#$t1 counter


#print sorted array
print:
    bgt         $t1,$s0,done        # more strings to output?  if no, fly

    lw          $t2,array($t0)      # get pointer to string
    # output the string
    li      $v0,4           #4 -> string    
    move        $a0,$t2
    syscall
    #jal        new_line

    lw          $t2,numarr($t0)      #get pointer to number    
    # output the number 
    li      $v0,1            # 1-> number   
    move    $a0, $t2
    syscall
    jal     new_line

    addi        $t0,$t0,4           # advance array index
    addi        $t1,$t1,1           # advance count
    j           print


# new_line -- output a newline char
new_line:
    la          $a0,newline
    li          $v0,4
    syscall
    jr          $ra

# program is done
done:
    li          $v0,10
    syscall

Regard.

mips

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0