Search code examples
assemblymips32

Selective Syscall and values crushings in a simple loop


this is a very simple loop allthough in the seventh repet (when the general amount number is bigger then 7 ,the syscall is starting to misbehave and its not printing the "enter_num" tag,also its crushing values on the data segment after this point. I don't understand the reason..



.data
.asciiz

Amount:"Enter amount of numbers " 
enter_num:"Enter number "
of:" of "
space:" "

.text


la $a1 0x10010000 #Loads address to $a1

#Ask for general number amount
la $a0 Amount
li $v0 4
syscall
li $v0 5
syscall

add $t1 $t1 $v0 #store the general amount of numbers in $t1

#Define counter $t2
li $t2 1

#store first general number
sb $t1 0($a1)#store byte in address
addi $a1 $a1 1 #Promote $a2 block address by 1 step

loop:

#Ask for array numbers

la $a0 enter_num
li $v0 4
syscall
la $a0,($t2)
li $v0 1
syscall
la $a0 of
li $v0 4
syscall
la $a0,($t1)
li $v0 1
syscall
la $a0 space
li $v0 4
syscall
li $v0 5
syscall

sb $v0 0($a1)#store byte in address



addi $a1 $a1 1#Promote $a1 address by 1 step

beq $t2 $t1 finish #loop ends when we reach the general amount number

addi $t2 $t2 1#promote $t2 counter

j loop



Solution

  • $a1 points to 0x10010000, that's the base of your data segment (where your ASCII strings are stored). By writing data there, you're effectively overwriting your strings.

    Try this: chose an amount of 30 numbers, enter 0 up to number #24, then 65 for number #25, 66 for number #26, etc... you should get something like this:

    Enter number 1 of 30 0
    ...
    Enter number 24 of 30 0
    Enter number 25 of 30 65
    Anter number 26 of 30 66
    ABter number 27 of 30 67
    ABCer number 28 of 30 68
    ABCDr number 29 of 30 69
    ABCDE number 30 of 30 70
    

    65 is the ASCII code for 'A', 66 for 'B', etc... you can see your data being overwritten as you go.

    One solution would be to store your numbers right after space. This works with spim:

    .data
    
    Amount:    .asciiz "Enter amount of numbers "
    enter_num: .asciiz "Enter number "
    of:        .asciiz " of "
    space:     .asciiz " "
    
    numbers:   .space 128   # <- some room for numbers here
    
    .text
    .globl main
    main:
    
      #la $a1 0x10010000    # <- replace this...
      la $a1 numbers        # <- ...with this
    
      # ...rest of the code here...