Sorry that I keep asking questions I feel so bad.
.data
.align 4
Table: .space 24
msg1: .asciiz "Please insert an integer: "
msg2: .asciiz " "
msg3: .asciiz "\nVector contents: "
.text
.globl main
main:
addi $s0,$zero,5
addi $t0,$zero,0
in:
li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
add $t1,$t0,$zero
sll $t1,$t0,2
add $t3,$v0,$zero
sw $t3,Table ( $t1 )
addi $t0,$t0,1
slt $t1,$s0,$t0
beq $t1,$zero,in
la $a0,Table
addi $a1,$s0,1 #a1=6
#call buble_sort
jal buble_sort
#print table
li $v0,4
la $a0,msg3
syscall
la $t0,Table
#s0=5
add $t1,$zero,$zero
printtable:
lw $a0,0($t0)
li $v0,1
syscall
li $v0,4
la $a0,msg2
syscall
addi $t0,$t0,4
addi $t1,$t1,1
slt $t2,$s0,$t1
beq $t2,$zero,printtable
li $v0,10
syscall
buble_sort:
#a0=address of table
#a1=sizeof table
add $t0,$zero,$zero #counter1( i )=0
loop1:
addi $t0,$t0,1 #i++
bgt $t0,$a1,endloop1 #if t0 < a1 break;
add $t1,$a1,$zero #counter2=size=6
loop2:
bge $t0,$t1,loop1 #j < = i
#slt $t3,$t1,$t0
#bne $t3,$zero,loop1
addi $t1,$t1,-1 #j--
mul $t4,$t1,4 #t4+a0=table[j]
addi $t3,$t4,-4 #t3+a0=table[j-1]
add $t7,$t4,$a0 #t7=table[j]
add $t8,$t3,$a0 #t8=table[j-1]
lw $t5,0($t7)
lw $t6,0($t8)
bgt $t5,$t6,loop2
#switch t5,t6
sw $t5,0($t8)
sw $t6,0($t7)
j loop2
endloop1:
jr $ra
This IS NOT my code, I am simply just trying to understand what happens at a certain spot
Few questions.. What does ALIGN and .SPACE do? How do you know how much space you need to allocate?
I understand most of the code (at least I think I do) But I'm stumped on the loop1 and loop2 label.
MAIN QUESTION: My question is when does the program ever GO back to loop1? If the counter for loop 1 starts at 1 (from addi) it needs to reach 6 to break. When is it ever called to increment +1 because in loop2
bge $t0,$t1,loop1
This is NEVER going to be true unless $t0 is incremented? $t1 value is the value of the array which is 6. I don't see the program ever going back to the loop1 ... so can someone explain to me how it does? The counter Has to reach 6 somehow because the program works fine. (Printing out assorted array from least to greatest)
With the MARS simulator (yes, I know "simulator" is redundant and I'll fix it as soon as people stop referring to ATM machines and PIN numbers), .align 4
will set the alignment to a multiple of sixteen (24
). For example, it will ensure that Table
will be on a sixteen-byte boundary like 0000
, 4110
, or fff0
. Alignment is usually because some CPUs work faster if n
-byte data elements are aligned on an n
-byte boundary (some will even raise a hardware exception if alignment is violated) but I'm not sure that's the case here since an alignment of sixteen is pretty "wide".
.space 24
simply allocates twenty-four bytes of space, in this case to hold the Table
data structure.
It appears that your initial loop (inputting the data) has a limit of six entries, based on the interaction between $s0
and $t0
.
The instruction sequence:
slt $t1,$s0,$t0
beq $t1,$zero,in
will branch back to in
as long as $s0
(= 5
) is less than $t0
, so $t0
iterates from 0
to 5
inclusive (six items).
That limit is how you would decide how much space to allocate (six 32-bit values would be twenty-four bytes).
And that loop will work. You state that:
bge $t0,$t1,loop1
: This is NEVER going to be true unless$t0
is incremented?
but, in fact, that's not quite right, it could also become true if you decrement $t1
and, lo and behold, there it is on the third code (non-':'
) line below:
loop2:
bge $t0,$t1,loop1 #j < = i
:
addi $t1,$t1,-1 #j--
:
j loop2
Thinking in terms of pseudo-code, it's the same as these two loops (in terms of how many iterations are done):
limit = 10, value = 0 limit = 10, value = 0
while value < limit: while value < limit:
value = value + 1 limit = limit - 1