[need Help] Assembly Language Array Sorting

  • Work-from-home

Lightman

TM Star
Aug 27, 2009
4,299
2,429
1,313
FrOnT Of YoUr EyEs
Hello how are you all?

Ok as title suggests that i need bit of help in assembly language (MIPS)
1: I need to write a program that finds the biggest/smallest value in the array.
I have managed to find the biggest value but cant figure out the smallest.
2: I thought to sort out the array by using Bubble Sort and than print out the first and the last value to solve problem (1). But i couldn't implement Bubble sort in mips so i dropped that idea. Let me know if any one can help either sorting the array in ascending order or finding the smallest value.


My Code For Finding Biggest Value(Its Working Fine)

Code:
.data
array: .word 3, 9, 2, 1, 8, 2, 4, 6, 3, 8
arraylength: .word 10
max: .word 0
 
.text
 
.globl main
 
main:
 
la $s0, array        # loading array
li $t1, 0            # index
add $t1, $t1, $zero    # max
lw $t5, arraylength    # number of elements
add $t2, $t2, $s0    # offset plus base
 
Loop:
 
beq $t0, $t5, Exit            # Loop control ($t0==$t5)
lw $t3, 0($t2)                # Getting First element of Array
add $t0, $t0, 1                # i++
add $t2, $t2, 4                # # update array address
slt $t4, $t1, $t3            # $t4 = 1 if $t1 is less than $t3, otherwise $t4 = 0
beq $t4, $zero, Loop        # branch to loop if value in $t4 is equal to value in $zero
add $t1, $t3, $zero    #
 
j Loop
 
Exit:
 
sw $t1, 0($t2)        # Storing Highest Number
 
move $a0, $t1
li $v0, 1
syscall
 
li $v0, 10        # syscall to terminate
syscall
 
  • Like
Reactions: ChoCo and yoursks

Mahen

Alhamdulillah
VIP
Jun 9, 2012
21,845
16,877
1,313
laнore
@Don i am not much in touch with Assembly, let will discuss with colleague and if there is someone then get back to you...
oops many 1st semester mein study ki ti :D ab tou kabhi touch hi nhi ki :s try karti ho /:) :p[DOUBLEPOST=1367040799][/DOUBLEPOST]
I am not into assembly language tho. mahen or @diya. could help or @yoursks
shukr hai me ko tag nhi kia /:) :p
 
  • Like
Reactions: Lightman and ChoCo

ChoCo

{ ' . ' } {C__C}
Super Star
Oct 29, 2012
6,169
6,637
363
MaRs
Hello how are you all?

Ok as title suggests that i need bit of help in assembly language (MIPS)
1: I need to write a program that finds the biggest/smallest value in the array.
I have managed to find the biggest value but cant figure out the smallest.
2: I thought to sort out the array by using Bubble Sort and than print out the first and the last value to solve problem (1). But i couldn't implement Bubble sort in mips so i dropped that idea. Let me know if any one can help either sorting the array in ascending order or finding the smallest value.


My Code For Finding Biggest Value(Its Working Fine)

Code:
.data
array: .word 3, 9, 2, 1, 8, 2, 4, 6, 3, 8
arraylength: .word 10
max: .word 0
 
.text
 
.globl main
 
main:
 
la $s0, array        # loading array
li $t1, 0            # index
add $t1, $t1, $zero    # max
lw $t5, arraylength    # number of elements
add $t2, $t2, $s0    # offset plus base
 
Loop:
 
beq $t0, $t5, Exit            # Loop control ($t0==$t5)
lw $t3, 0($t2)                # Getting First element of Array
add $t0, $t0, 1                # i++
add $t2, $t2, 4                # # update array address
slt $t4, $t1, $t3            # $t4 = 1 if $t1 is less than $t3, otherwise $t4 = 0
beq $t4, $zero, Loop        # branch to loop if value in $t4 is equal to value in $zero
add $t1, $t3, $zero    #
 
j Loop
 
Exit:
 
sw $t1, 0($t2)        # Storing Highest Number
 
move $a0, $t1
li $v0, 1
syscall
 
li $v0, 10        # syscall to terminate
syscall
ok! I was into Assembly language! but i forgot it all!
Let me ask someone who can really help you out :)
Will take 2 to 3 days though!
 

Lightman

TM Star
Aug 27, 2009
4,299
2,429
1,313
FrOnT Of YoUr EyEs
Hi, thanks all, it took me so many hours but its finally solved. Now i know why we need to appreciate high level languages.

I m providing the code below , if any one needs this in future.

Final Structure for finding biggest and smaller value.

Code:
#<!---------------------------------------------------------!>
 
biggestNumber:
    la $t0, array        # loading Array address
    lw $t1, arraylength    # Loop Control
    lw $t2, ($t0)        # initialize min 0
    lw $t3, ($t0)        # initialize max 0
    add $t0, $t0,4        # starting array
    add $t1, $t1,-1        # arraylength -1
 
    loop:
    lw $t4,($t0)            # loading value from array
    bge $t4,$t2,minCheck    # Condition check if array[i] >= min
    move $t2,$t4            # copy a[i] to min
 
minCheck:
 
    ble $t4,$t3,maxCheck    # skip if a[i] <= max
    move $t3,$t4            # move array[i] to max
 
 
maxCheck:
 
    add $t1,$t1,-1        # arraylength-1
    add $t0,$t0,4        # update Array
    bnez $t1, loop        # continue if arrayLength > 0
 
    beq $v0, 2, printMax
 
 
    la $a0,minT          # out "min = "
    li $v0,4   
    syscall       
 
         
    move $a0,$t2        # printout min
    li $v0,1
    syscall
 
 
printMax:
 
    la $a0,maxT        # print out "max = "
    li $v0,4
    syscall
 
    move $a0,$t3        # printout max
    li $v0,1
    syscall
 
    la $a0,bl        # Line Break
    li $v0,4   
    syscall
 
#<!---------------------------------------------------------!>
 
  • Like
Reactions: hoorain and Mahen

Mahen

Alhamdulillah
VIP
Jun 9, 2012
21,845
16,877
1,313
laнore
Hi, thanks all, it took me so many hours but its finally solved. Now i know why we need to appreciate high level languages.

I m providing the code below , if any one needs this in future.

Final Structure for finding biggest and smaller value.

Code:
#<!---------------------------------------------------------!>
 
biggestNumber:
    la $t0, array        # loading Array address
    lw $t1, arraylength    # Loop Control
    lw $t2, ($t0)        # initialize min 0
    lw $t3, ($t0)        # initialize max 0
    add $t0, $t0,4        # starting array
    add $t1, $t1,-1        # arraylength -1
 
    loop:
    lw $t4,($t0)            # loading value from array
    bge $t4,$t2,minCheck    # Condition check if array[i] >= min
    move $t2,$t4            # copy a[i] to min
 
minCheck:
 
    ble $t4,$t3,maxCheck    # skip if a[i] <= max
    move $t3,$t4            # move array[i] to max
 
 
maxCheck:
 
    add $t1,$t1,-1        # arraylength-1
    add $t0,$t0,4        # update Array
    bnez $t1, loop        # continue if arrayLength > 0
 
    beq $v0, 2, printMax
 
 
    la $a0,minT          # out "min = "
    li $v0,4 
    syscall     
 
       
    move $a0,$t2        # printout min
    li $v0,1
    syscall
 
 
printMax:
 
    la $a0,maxT        # print out "max = "
    li $v0,4
    syscall
 
    move $a0,$t3        # printout max
    li $v0,1
    syscall
 
    la $a0,bl        # Line Break
    li $v0,4 
    syscall
 
#<!---------------------------------------------------------!>
thanks for sharing with us. : ) shukr hai ap ny kudi solve kar liya problem /:) :p
 
Top