dsotelo91 0 Newbie Poster

Hi there. I am trying to get a selection sort working on assembly language, using the Irvine32.lib. This is my first time taking an assembly language class and I am struggling with this program a lot. Here is the code.

TITLE MASM Template						(main.asm)

INCLUDE Irvine32.inc

array1 DWORD 100 DUP(?)
str1 BYTE "Enter total number of integers to be sorted: ",0
str2 BYTE "Enter numbers to be sorted. Press enter after each integer: ", 0
intval DWORD ?

main PROC
	call Clrscr				;Ask user for number of integers to be sorted
	mov edx, OFFSET str1
	call WriteString
	call crlf
	call ReadInt
	call Crlf

	mov esi, OFFSET array1		;Ask user for integers to be sorted
	mov intval, eax			;sets the number of integers inputed by the user into intval
	mov ecx, intval			;this is so only the filled elements in the array are displayed
	call PromptForIntegers 

	call DisplayArray

	call SelectionSort

	call DisplayArray
main ENDP

PromptForIntegers PROC USES ecx edx esi
	mov edx, OFFSET str2
	call WriteString
	call Crlf

	call ReadInt
	call Crlf
	mov [esi], eax
	add esi, TYPE DWORD
	loop L1

PromptForIntegers ENDP

SelectionSort PROC 	;receives intval
		mov eax, 0							;i = 0
		;dec intval							;intval is now n - 1. Cannot do. Takes a element from the array.
outer:	cmp eax, intval						;for (i = 0, i < n - 1; i++)
		jae finish

		mov ecx, eax						;minIndex = i
		inc eax								;eax = i + 1 now

		mov ebx, eax						;j = i + 1
inner:	cmp ebx, intval
		jae last

		push eax
		mov eax, array1[ebx]
		.IF (eax < array1[ecx])				;if (arr[j] < arr[minIndex])
				mov ecx, ebx				;minIndex = j
		pop eax
		push ebx

		.IF (ecx != eax)					;if (minIndex != i)
				mov edx, array1[eax]		;tmp = arr[i]
				mov ebx, array1[eax]
				mov ebx, array1[ecx]		;arr[1] = arr[minIndex];
				mov array1[ecx], edx		;arr[minIndex] = tmp;

		pop ebx

		inc ebx		;j++
		inc eax		;i++
		jmp outer
SelectionSort ENDP

DisplayArray PROC
	mov esi, OFFSET array1	
	mov ecx, intval 
	mov ebx, TYPE array1
	call DumpMem
DisplayArray ENDP
END main

It is based on this c++ code.

void selectionSort(int arr[], int n) {

      int i, j, minIndex, tmp;    

      for (i = 0; i < n - 1; i++) {

            minIndex = i;

            for (j = i + 1; j < n; j++)

                  if (arr[j] < arr[minIndex])

                        minIndex = j;

            if (minIndex != i) {

                  tmp = arr[i];

                  arr[i] = arr[minIndex];

                  arr[minIndex] = tmp;




Thanks in advance!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.