1
votes

I have to create a program in assembly that takes a user's input as a maximum array size then let's the user create an array of that size. I'm supposed to buffer that value to a maximum of 1000 array items (all ints) Then I have to run a selection sort on the array and output.

I've found the following selection sort on the IBM website.

    .section .data

   array:
      .byte  89, 10, 67, 1, 4, 27, 12, 34, 86, 3

   array_end:
      .equ ARRAY_SIZE, array_end - array

   array_fmt:
      .asciz "  %d"

   usort_str:
      .asciz "unsorted array:"

   sort_str:
      .asciz "sorted array:"

   newline:
      .asciz "\n"


.section .text
.globl main

main:
      pushl $usort_str
      call  puts
      addl  $4, %esp

      pushl $ARRAY_SIZE
      pushl $array
      pushl $array_fmt
      call  print_array10
      addl  $12, %esp

      pushl $ARRAY_SIZE
      pushl $array
      call  sort_routine20

# Adjust the stack pointer
      addl  $8, %esp

      pushl $sort_str
      call  puts
      addl  $4, %esp

      pushl $ARRAY_SIZE
      pushl $array
      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

I've gone through it and I can make sense of 90% of what's going on so I'm happy with that. What I have no idea where to even begin is how do I take the user input and create an array with it? And how do I use the buffer to set a limit to the array size? Any help is very much appreciated!

1

1 Answers

1
votes

This program is not interactive. The "input" is given in the line array

array:
      .byte  89, 10, 67, 1, 4, 27, 12, 34, 86, 3

and can only be changes during compile time. If you need to take a user input, you would need to make calls to the appropriate kernel functions or standard library functions.

http://linux.die.net/man/2/read with the stdin file descriptor (very low level, but you are writeing assembler) or http://www.manpagez.com/man/3/scanf/ (the C function). You also get an overview with "man scanf" in the command line.

You put the arguments you want to give to the functions in reverse order on the stack, call the function, and readjust the stack. How the arguments are given to the functions is depending on the architecture and the operating system. It is defined in the application binary interface ("ABI"). See http://www.scribd.com/doc/48244725/abi386-4 under "function calling sequence", assuming you are on a i386.

You can reserve 1000 bytes in the .bss section with

.section .bss
buffer:  .space 1000

Or you can use "malloc".