Here is my code
.section .data
values:
.int 0
array_end:
.equ ARRAY_SIZE, array_end - values
array_fmt:
.asciz " %d"
usort_str:
.asciz "unsorted array:"
sort_str:
.asciz "sorted array:"
newline:
.asciz "\n"
maxvalue:
.int 1000
format:
.asciz "%d"
message:
.asciz "Please enter a maximum size for the array to be sorted."
output:
.asciz "The length is '%d' \n"
val_fmt:
.asciz " %d"
.section .bss
.comm buffer, 4000
.section .text
.globl main
main:
pushl $message
call printf
addl $4, %esp
pushl $maxvalue
pushl $format
call scanf
addl $8, %esp
movl $buffer, %edi
movl maxvalue, %edi
pushl maxvalue
pushl $output
call printf
addl $8, %esp
movl maxvalue, %ecx
someLoop:
cmpl $0, %ecx
je endLoop
pushl $values
pushl $val_fmt
call scanf
addl $8, %esp
sub $1, %ecx
jmp someLoop
movl $0, %ecx
endLoop:
pushl $usort_str
call puts
addl $4, %esp
#selection sort
pushl $ARRAY_SIZE
pushl $values
pushl $array_fmt
call print_array10
addl $12, %esp
pushl $ARRAY_SIZE
pushl $values
call sort_routine20
# Adjust the stack pointer
addl $8, %esp
pushl $sort_str
call puts
addl $4, %esp
pushl $ARRAY_SIZE
pushl $values
pushl $array_fmt
call print_array10
addl $12, %esp
jmp _exit
print_array10:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
movl 8(%ebp), %edx
movl 12(%ebp), %ebx
movl 16(%ebp), %ecx
movl $0, %esi
push_loop:
movl %ecx, -4(%ebp)
movl 8(%ebp), %edx
xorl %eax, %eax
movb (%ebx, %esi, 1), %al
pushl %eax
pushl %edx
call printf
addl $8, %esp
movl -4(%ebp), %ecx
incl %esi
loop push_loop
pushl $newline
call printf
addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
sort_routine20:
pushl %ebp
movl %esp, %ebp
# Allocate a word of space in stack
subl $4, %esp
# Get the address of the array
movl 8(%ebp), %ebx
# Store array size
movl 12(%ebp), %ecx
decl %ecx
# Prepare for outer loop here
xorl %esi, %esi
outer_loop:
# This stores the min index
movl %esi, -4(%ebp)
movl %esi, %edi
incl %edi
inner_loop:
cmpl $ARRAY_SIZE, %edi
jge swap_vars
xorb %al, %al
movl -4(%ebp), %edx
movb (%ebx, %edx, 1), %al
cmpb %al, (%ebx, %edi, 1)
jge check_next
movl %edi, -4(%ebp)
check_next:
incl %edi
jmp inner_loop
swap_vars:
movl -4(%ebp), %edi
movb (%ebx, %edi, 1), %dl
movb (%ebx, %esi, 1), %al
movb %dl, (%ebx, %esi, 1)
movb %al, (%ebx, %edi, 1)
incl %esi
loop outer_loop
movl %ebp, %esp
popl %ebp
ret
exit:
movl $1, %eax
movl 0, %ebx
int $0x80
push $0
call exit
This program does:
ask user what size of array to build
request int values and enter them into array until array is of correct size
sort values using selection sort
The sort works perfectly but I am having problems with building the array.
As it stands, it seems like the values I'm using scanf to read into the array are overwritting the last value input so if I enter: 1, 2, 3, 4
the array only contains 4.
I know I have to offset the array by 4 bytes each time I want to write to it, but I have no idea how to do this.
Closer look at my array building loop is
someLoop:
cmpl $0, %ecx
je endLoop
pushl $values
pushl $val_fmt
call scanf
addl $8, %esp
sub $1, %ecx
jmp someLoop
movl $0, %ecx
I'm looking through every resource I have but can't find how to build arrays from user input so ANY help or suggestions would go a long way.
Thank you for your time