Insertion sort in Assembly - sorting

So I'm coding out an insertion sort (in assembly) based on this high level code:
void insertionSort(int data[ ], int arraySize) {
int insert;
int moveItem;
for(int next=1; next<arraySize; next++) {
insert=data[next]; //store the value in the current element
moveItem=next; //initialize location to place element
while((moveItem>0)&&(data[moveItem-1]>insert)) {
//shift element one slot to the right
data[moveItem]=data[moveItem-1];
moveItem--;
} //end while
data[moveItem]=insert;
} //end for
} //end insertionSort
There are exactly 20 random numbers in an array called myArray. I cannot use an of the decision derivatives that come in the library that comes with our book. So basically movs, cmps, loops, and jumps
Here's what I got. I had it sorting the first of 20 random numbers earlier, but I've confused myself to death and have no idea what I'm doing any more. It crashes when it gets to the insertion sort method. Help please.
TITLE Insertion Sort (main.asm)
INCLUDE Irvine32.inc
.data
elems = 20
myArray sdword elems dup(0)
str1 byte "Press enter" ,0
str2 byte "The array now is ",0
next sdword 1
start sdword ?
.code
main PROC
call Clrscr
call CreateRandom
call Display
call InsertionSort
call Display
exit
main ENDP
CreateRandom PROC
;;;;Creates 20 random numbers and populates myArray;;;;;
call Randomize
mov ecx, 20
mov edx, OFFSET myArray
L1:
call Random32 ;create random number
mov [edx], eax ; move random number to the appropriate spot in the array
add edx, 4 ; increase the address of what it is pointing to
loop L1
mov edx, OFFSET str1 ; "press enter to continue"
call WriteString
call ReadInt
call Crlf
ret
CreateRandom ENDP
Display PROC
;;;; Displays current form of the array myArray;;;;;
mov edx, OFFSET str2 ; "The array now is:"
call WriteString ; write string
call Crlf
mov esi, OFFSET myArray ; offset of the array
mov ecx, 20 ; index of the loop
L2:
mov eax, [esi] ; move array at that point to eax
call WriteDec ; print out the array as a decimal
call Crlf ; next line
add esi, 4 ; next element in the array
loop L2
call Crlf
ret
Display ENDP
InsertionSort PROC
mov ecx, 19
mov edx, OFFSET myArray
mov ebx, OFFSET myArray ; eax=next
add ebx, 4 ;moves up the array to second element comparable to next
outterloop:
mov esi, [ebx] ; esi=data[next]
mov eax, ebx ;movelterm=ebx
L1:
mov edx, [eax-4] ;move the number that is greater into edx
mov [eax], edx ;move the number into that 2nd element of the
sub eax, 4
mov esi, [eax]
cmp eax, [edx]
JNG endinner ; if the address is not greater than the first address, skip to the end
mov edx, [eax-4]
cmp edx, esi ; if the address is greater, than it already sorted, skip to end
JG endinner
loop L1
endinner:
mov [eax], esi ; move first to the second to finish the sort
add ebx, 4 ;move to the next element of the array
inc next ;counting outside loop
cmp ecx, next
JNE outterloop ;return to top of for loop
ret
InsertionSort ENDP
END main

I haven't examined your code in detail, but I notice that InsertionSort seems to be using edx for two different purposes at once: as a pointer into the array, and to hold one of the values from the array. This will certainly break even if nothing else is wrong.
So, at the start of InsertionSort you say mov edx, OFFSET myArray -- it's a pointer into the array. Then, a few lines later, mov edx, [eax-4] -- oops, no, it's a value from the array. And a few lines later again, cmp eax, [edx] -- oh, no, now it's a pointer into the array again.
Perhaps that last instruction should be cmp edx, [eax] or something? Because eax does seem to be a pointer into the array here.

Related

I'm unsure what the problem with my assembly code it works until eax is popped and replaced by a register

; Input x and y, output min of the two numbers
.586
.MODEL FLAT
INCLUDE io.h
.STACK 4096
.DATA
number DWORD ?
array DWORD 20, 15, 62, 40, 18
nbrElts DWORD 5
prompt BYTE "Enter value:", 0
string BYTE 80 DUP (?)
resultLbl BYTE "Position", 0
result BYTE 11 DUP (?), 0
.CODE
_MainProc PROC
input prompt, string, 20 ; read ASCII characters
atod string ; convert to integer
mov number, eax ; store in memory
push nbrElts ; 3rd parameter (# of elements in array)
lea eax, array ; 2nd parameter (address of array)
push eax
push number ; 1st parameter (value from user)
call searchArray ; searchArray(number, array, 5)
add esp, 12
dtoa result, eax ; convert to ASCII characters
output resultLbl, result ; output label and result
mov eax, 0 ; exit with return code 0
ret
_MainProc ENDP
; searchArray(int x, array, int y)
;
searchArray PROC
push ebp ; save base pointer
mov ebp, esp ; establish stack frame
push eax ; save registers
push ebx
push esi
push ecx
push edx
mov ebx, [ebp+8] ; x, value from user
mov esi, [ebp+12] ; address of array
mov ecx, [ebp+16] ; y, number of elements
mov edx, 1
mov ecx, 5
forLoop:
mov eax, [esi] ; a[i]
cmp eax, ebx ; eax = ebx ?
je isEqual
;cmp eax, ebx
add esi, 4
inc edx
loop forLoop
;mov eax, 0
cmp edx, 6
je notEqual
isEqual:
mov eax, edx
jmp exitCode
notEqual:
mov eax, 0
jmp exitCode
exitCode:
mov eax, edx
pop edx ; restore EBP
pop ecx ; restore EAX
pop esi
pop ebx
pop ebp
ret ; return
searchArray ENDP
END ; end of source code
The pops at the end of the function need to match the pushes at the beginning of the function. If they don't match, the stack pointer ends up in the wrong place and the ret returns to the wrong place.
In your case, you have an extra push without a corresponding pop.
The reason to push registers at the beginning and pop them at the end is to preserve their values. But you don't want to preserve the value of eax. You want to return a different value, the result of the function. So there is absolutely no reason to push eax.

Finding Smallest Number in List

My goal in this code is to find the smallest number in the list. I used bubble sort method in this case; unfortunately, the code is not giving me the smallest/minimum number. Please take a look, Thanks:
include irvine32.inc
.data
input byte 100 dup(0)
stringinput byte "Enter any string: ",0
totallength byte "The total length is: ",0
minimum byte "The minimum value is: ",0
.code
stringLength proc
push ebp
mov ebp, esp
push ebx
push ecx
mov eax, 0
mov ebx, [ebp+8]
L1:
mov ecx, [ebx] ;you can use ecx, cx, ch, cl
cmp ecx, 0 ;you can use ecx, cx, ch, cl
JE L2
add ebx, 1
add eax, 1
jmp L1
L2:
pop ecx
pop ebx
mov ebp, esp
pop ebp
ret 4
stringLength endp
BubbleSort PROC uses ECX
push edx
xor ecx,ecx
mov ecx, 50
OUTER_LOOP:
push ecx
xor ecx,ecx
mov ecx,14
mov esi, OFFSET input
COMPARE:
xor ebx,ebx
xor edx,edx
mov bl, byte ptr ds:[esi]
mov dl, byte ptr ds:[esi+1]
cmp bl,dl
jg SWAP1
CONTINUE:
add esi,2
loop COMPARE
mov esi, OFFSET input
pop ecx
loop OUTER_LOOP
jmp FINISHED
SWAP1:
xchg bl,dl
mov byte ptr ds:[esi+1],dl
mov byte ptr ds:[esi],bl
jmp CONTINUE
FINISHED:
pop edx
ret 4
BubbleSort ENDP
main proc
call clrscr
mov edx, offset stringinput
call writeString
mov edx, offset input
call writeString
call stringLength
mov edx, offset input
mov ecx, sizeof input
call readstring
call crlf
mov edx,offset totallength
call writestring
call writedec
call crlf
mov edx, offset minimum
call crlf
call writeString
push offset input
call BubbleSort
mov edx, offset input
call writeString
call crlf
exit
main endp
end main
I haven't looked over your code, because sorting is an over complicated method for what you want to do. Not only that, but most of us don't pay too much attention to uncommented code. Just takes to long to figure out what you're trying to do.
Simply iterate through the entire list and start with 255 (FFH) in AL let's say. Each time you come across a number that is smaller than the one in AL, then replace it with that value and then when loop is finished, AL will have the lowest value.
If you need to know where it is in the list, you could maybe use AH which would be the difference between start address and current address. Knowledge of the instruction set is essential as finding the length of the string can be simplified by;
mov di, input ; Point to beginning of buffer
mov cx, -1 ; for a maximum of 65535 characters
xor al, al ; Looking for NULL
rep scasb
neg cx
dec cx ; CX = length of string.
Remember, ES needs to point to #DATA

How to echo memory location use NASM [duplicate]

I am looking for a way to print an integer in assembler (the compiler I am using is NASM on Linux), however, after doing some research, I have not been able to find a truly viable solution. I was able to find a description for a basic algorithm to serve this purpose, and based on that I developed this code:
global _start
section .bss
digit: resb 16
count: resb 16
i: resb 16
section .data
section .text
_start:
mov dword[i], 108eh ; i = 4238
mov dword[count], 1
L01:
mov eax, dword[i]
cdq
mov ecx, 0Ah
div ecx
mov dword[digit], edx
add dword[digit], 30h ; add 48 to digit to make it an ASCII char
call write_digit
inc dword[count]
mov eax, dword[i]
cdq
mov ecx, 0Ah
div ecx
mov dword[i], eax
cmp dword[i], 0Ah
jg L01
add dword[i], 48 ; add 48 to i to make it an ASCII char
mov eax, 4 ; system call #4 = sys_write
mov ebx, 1 ; file descriptor 1 = stdout
mov ecx, i ; store *address* of i into ecx
mov edx, 16 ; byte size of 16
int 80h
jmp exit
exit:
mov eax, 01h ; exit()
xor ebx, ebx ; errno
int 80h
write_digit:
mov eax, 4 ; system call #4 = sys_write
mov ebx, 1 ; file descriptor 1 = stdout
mov ecx, digit ; store *address* of digit into ecx
mov edx, 16 ; byte size of 16
int 80h
ret
C# version of what I want to achieve (for clarity):
static string int2string(int i)
{
Stack<char> stack = new Stack<char>();
string s = "";
do
{
stack.Push((char)((i % 10) + 48));
i = i / 10;
} while (i > 10);
stack.Push((char)(i + 48));
foreach (char c in stack)
{
s += c;
}
return s;
}
The issue is that it outputs the characters in reverse, so for 4238, the output is 8324. At first, I thought that I could use the x86 stack to solve this problem, push the digits in, and pop them out and print them at the end, however when I tried implementing that feature, it flopped and I could no longer get an output.
As a result, I am a little bit perplexed about how I can implement a stack in to this algorithm in order to accomplish my goal, aka printing an integer. I would also be interested in a simpler/better solution if one is available (as it's one of my first assembler programs).
One approach is to use recursion. In this case you divide the number by 10 (getting a quotient and a remainder) and then call yourself with the quotient as the number to display; and then display the digit corresponding to the remainder.
An example of this would be:
;Input
; eax = number to display
section .data
const10: dd 10
section .text
printNumber:
push eax
push edx
xor edx,edx ;edx:eax = number
div dword [const10] ;eax = quotient, edx = remainder
test eax,eax ;Is quotient zero?
je .l1 ; yes, don't display it
call printNumber ;Display the quotient
.l1:
lea eax,[edx+'0']
call printCharacter ;Display the remainder
pop edx
pop eax
ret
Another approach is to avoid recursion by changing the divisor. An example of this would be:
;Input
; eax = number to display
section .data
divisorTable:
dd 1000000000
dd 100000000
dd 10000000
dd 1000000
dd 100000
dd 10000
dd 1000
dd 100
dd 10
dd 1
dd 0
section .text
printNumber:
push eax
push ebx
push edx
mov ebx,divisorTable
.nextDigit:
xor edx,edx ;edx:eax = number
div dword [ebx] ;eax = quotient, edx = remainder
add eax,'0'
call printCharacter ;Display the quotient
mov eax,edx ;eax = remainder
add ebx,4 ;ebx = address of next divisor
cmp dword [ebx],0 ;Have all divisors been done?
jne .nextDigit
pop edx
pop ebx
pop eax
ret
This example doesn't suppress leading zeros, but that would be easy to add.
I think that maybe implementing a stack is not the best way to do this (and I really think you could figure out how to do that, saying as how pop is just a mov and a decrement of sp, so you can really set up a stack anywhere you like by just allocating memory for it and setting one of your registers as your new 'stack pointer').
I think this code could be made clearer and more modular if you actually allocated memory for a c-style null delimited string, then create a function to convert the int to string, by the same algorithm you use, then pass the result to another function capable of printing those strings. It will avoid some of the spaghetti code syndrome you are suffering from, and fix your problem to boot. If you want me to demonstrate, just ask, but if you wrote the thing above, I think you can figure out how with the more split up process.
; Input
; EAX = pointer to the int to convert
; EDI = address of the result
; Output:
; None
int_to_string:
xor ebx, ebx ; clear the ebx, I will use as counter for stack pushes
.push_chars:
xor edx, edx ; clear edx
mov ecx, 10 ; ecx is divisor, devide by 10
div ecx ; devide edx by ecx, result in eax remainder in edx
add edx, 0x30 ; add 0x30 to edx convert int => ascii
push edx ; push result to stack
inc ebx ; increment my stack push counter
test eax, eax ; is eax 0?
jnz .push_chars ; if eax not 0 repeat
.pop_chars:
pop eax ; pop result from stack into eax
stosb ; store contents of eax in at the address of num which is in EDI
dec ebx ; decrement my stack push counter
cmp ebx, 0 ; check if stack push counter is 0
jg .pop_chars ; not 0 repeat
mov eax, 0x0a
stosb ; add line feed
ret ; return to main
; eax = number to stringify/output
; edi = location of buffer
intToString:
push edx
push ecx
push edi
push ebp
mov ebp, esp
mov ecx, 10
.pushDigits:
xor edx, edx ; zero-extend eax
div ecx ; divide by 10; now edx = next digit
add edx, 30h ; decimal value + 30h => ascii digit
push edx ; push the whole dword, cause that's how x86 rolls
test eax, eax ; leading zeros suck
jnz .pushDigits
.popDigits:
pop eax
stosb ; don't write the whole dword, just the low byte
cmp esp, ebp ; if esp==ebp, we've popped all the digits
jne .popDigits
xor eax, eax ; add trailing nul
stosb
mov eax, edi
pop ebp
pop edi
pop ecx
pop edx
sub eax, edi ; return number of bytes written
ret

How do you do a selection sort in MASM Assembly?

So I'm trying to sort an array in ascending order, and it just doesn't seem to work for me. I know my swap procedure is wrong, and my minIndex procedure only works half the time, but the randomly filled array generates just fine.
Working Code Thanks to vitsoft:
include Irvine32.inc
; Constants
NUM_INTEGERS = 20
NUM_RANGE = 99d
TO_ENSURE_NOT_ZERO = 1d
.data
; Declare Arrays
; Declare Array of 20 Integers
array byte NUM_INTEGERS dup(?)
; Displayed Annotation for Unsorted Array
unsortedArrayText byte "Randomly Generated Array, Unsorted: ", 0dh, 0ah, 0
; Displayed Annotation for Sorted Array
sortedArrayText byte "Randomly Generated Array, Sorted from Lowest to Highest: ", 0dh, 0ah, 0
.code
main PROC
; Get Random Values for Array
call fillArrayRandomly
; Display Array with Annotation
mov edx, offset unsortedArrayText
call WriteString
mov ecx, offset array
push ecx
call displayArray
; Sort Array
mov esi, offset array
mov ecx, NUM_INTEGERS
call findMinimum
mov edi, offset array
mov ecx, NUM_INTEGERS
call selectionSort
; Display Sorted Array with Annotation
mov edx, offset sortedArrayText
call WriteString
mov ecx, offset array
push ecx
call displayArray
; Exit Program
exit
main endp
; Fill Array with Random Values
fillArrayRandomly PROC
; Set Counter to NUM_INTEGERS and Index to 0
mov eax, 0
mov ecx, NUM_INTEGERS
mov esi, 0
getRandomNumber:
; Fill Array with Random Numbers and Increment ESI for Offset
mov ah, NUM_RANGE
call RandomRange
add ah, TO_ENSURE_NOT_ZERO
testUniqueness:
mov al, array [esi]
cmp al, ah
jne uniqueNumber
loop getRandomNumber
uniqueNumber:
mov array [esi], ah
inc esi
loop getRandomNumber
ret
fillArrayRandomly ENDP
; Display Array
displayArray PROC
pushad
mov eax, 0
mov ecx, NUM_INTEGERS
mov esi, 0
display:
mov al, array[esi]
call WriteDec
mov al, TAB
call WriteChar
inc esi
loop display
popad
call Crlf
ret
displayArray ENDP
; Selection Sort
selectionSort PROC
dec ecx
mov ebx, edi
mov edx, ecx
startOuterLoop:
mov edi, ebx
mov esi, edi
inc esi
push ecx
mov ecx, edx
startInnerLoop:
mov al, [esi]
cmp al, [edi]
pushf
inc esi
inc edi
popf
jae doNotSwap
call swap
doNotSwap:
loop startInnerLoop
pop ecx
loop startOuterLoop
ret
selectionSort ENDP
; Find Minimum Index
findMinimum PROC
mov edi, esi
minimumIndex:
mov al, [esi]
cmp al, [edi]
jae skip
mov edi, esi
skip:
inc esi
loop minimumIndex
ret
findMinimum ENDP
; Swap
swap PROC
mov al, [esi - 1]
mov ah, [edi - 1]
mov [esi - 1], ah
mov [edi - 1], al
ret
swap ENDP
END main
LOAD EFFECTIVE ADDRESS computes address of 2nd operand and moves it to the first operand.
lea esi,[edi+1] can be replaced with
mov esi,edi
inc esi
if you don't mind that INC will also change ZERO flag.
You should learn cmpsb and other string instructions ASAP, they are very useful.
cmpsb can be replaced with
MOV AL,[ESI]
CMP AL,[EDI]
PUSHF
INC ESI
INC EDI
POPF
After selectionSort has been called, the minimal value will be in the first field of your array.
Nevertheless, if your assignment was to find a pointer to the minimal value of array, here you are:
findMinimum PROC ; Return EDI=offset of leftmost byte in array with minimal value
; Input: ESI is offset of an byte array, ECX is the size of array
MOV EDI,ESI ; Initialize pointer with so far minimal value
next:MOV AL,[ESI] ; Load next value
CMP AL,[EDI] ; Compare with minimal value so far
JAE skip
MOV EDI,ESI ; Value loaded from [ESI] was below, so ESI is better candidate
skip:INC ESI ; Advance offset in array
LOOP next
RET
findMinimum ENDP
.data
unsortedArrayText DB "Randomly Generated Array, Unsorted: ", 0dh, 0ah, 0
sortedArrayText DB "Randomly Generated Array, Sorted from Lowest to Highest: ", 0dh, 0ah, 0
array DB "An array of bytes (characters) which will be sorted"
NewLine DB 0dh, 0ah, 0
NUM_INTEGERS EQU NewLine - array ; Number of integers (characters) in array
.code
main PROC
; Display Array with Annotation
; mov edx, offset unsortedArrayText
; call WriteString
ConsoleWrite unsortedArrayText
; push offset array
; call displayArray
ConsoleWrite array
; Sort Array
; push offset array
; call selectionSort
MOV EDI,array
MOV ECX,NUM_INTEGERS
CALL selectionSort
; Display Sorted Array with Annotation
; mov edx, offset sortedArrayText
; call WriteString
ConsoleWrite sortedArrayText
; push offset array
; call displayArray
ConsoleWrite array
; Exit Program
;exit
TerminateProgram
main endp
; Selection Sort
selectionSort PROC ; Bubble sort ECX byte array pointed to with EDI
DEC ECX ; Number of compare is NUM_INTEGERS-1
MOV EBX,EDI ; Save array pointer to EBX
MOV EDX,ECX ; Save number of compare to EDX
OuterLoop:
MOV EDI,EBX ; Restore array pointer
LEA ESI,[EDI+1] ; Neibourghing field
PUSH ECX ; Save OuterLoop counter on stack
MOV ECX,EDX ; Initialize InnerLoop counter
InnerLoop:
CMPSB ; compare the first byte [EDI] with its neibourgh [ESI], advance EDI,ESI
JAE NoSwap
CALL swap
NoSwap:
LOOP InnerLoop
POP ECX ; Restore OuterLoop counter
LOOP OuterLoop
RET
selectionSort ENDP
swap PROC ; Swap bytes at [EDI-1] and [ESI-1]
MOV AL,[ESI-1]
MOV AH,[EDI-1]
MOV [ESI-1],AH
MOV [EDI-1],AL
RET
swap ENDP
This worked well, statically defined array was sorted in few miliseconds:
C:\ASM>bubblesortexample.exe
Randomly Generated Array, Unsorted:
An array of bytes (characters) which will be sorted
Randomly Generated Array, Sorted from Lowest to Highest:
()Aaaaabbcccdeeeefhhhiillnoorrrrrssstttwwyy
C:\ASM>

Selection sort procedure in assembly

I think I'm having trouble with my swap, and how I'm accessing the elements in my array. Right now, all of the code runs, but the list does not change after the sort. He's the high level sort I'm trying to implement
for(k=0; k<request-1; k++) {
i = k;
for(j=k+1; j<request; j++) {
if(array[j] > array[i])
i = j;
}
exchange(array[k], array[i]);
}
Here's the assembly code. Note: the assignment is about pushing and popping elements on/off the stack, so I can't change the parameters.
;this is a library with macros for things like printing numbers and strings
INCLUDE Irvine32.inc
MIN = 10 ;lower range limit
MAX = 200 ;upper range limit
LRANGE = 100
HRANGE = 999
.data
;title, intro, and prompts
title_1 BYTE "PROGRAMMING ASSIGNMENT 5: RANDOM GEN/SORT", 0dh, 0ah, 0
intro_1 BYTE "This program generates random numbers in the range (100 - 999),", 0dh, 0ah
BYTE "displays the original list, sorts the list, and calculates the median value.", 0dh, 0ah
BYTE "Finally, it displays the sorted list in descending order.", 0dh, 0ah, 0
prompt_1 BYTE "How many numbers should be generated? (10 - 200): ", 0
error_1 BYTE "Out of range.", 0dh, 0ah, 0
display_1 BYTE "List of random numbers: ", 0dh, 0ah, 0
display_2 BYTE "The median is: ", 0
display_3 BYTE "The sorted list: ", 0dh, 0ah, 0
;placeholders for user entries and calculated data
randArray DWORD MAX DUP(?)
userNum DWORD ? ;integer to be entered by user
;strings for posting results
goodBye_1 BYTE "Thank you for using the Gen/sort-ulator! Good-bye!", 0
.code
main PROC
call Randomize
;Title Screen
push OFFSET title_1
push OFFSET intro_1
call Intro
;Get and validate user numbers
push OFFSET error_1
push OFFSET prompt_1
push OFFSET userNum
call GetData
;Fill Array with random numbers
push OFFSET randArray
push userNum
call FillArray
;display unsorted results
push OFFSET randArray
push userNum
push OFFSET display_1
call DisplayList
;sort the results
push OFFSET randArray
push userNum
call SortList
;display the median
push OFFSET randArray
push userNum
push OFFSET display_2
call median
;display sorted results
push OFFSET randArray
push userNum
push OFFSET display_3
call DisplayList
;Say "goodbye"
push OFFSET goodBye_1
call Goodbye
exit ; exit to operating system
main ENDP
;-------------------------------------------------------
;Gives an Intro to the program
; Receives parameters on the system stack (in the order pushed):
; Address of the title
; Address of the intro
;post: intro displayed
;registers: none
;-------------------------------------------------------
Intro PROC
pushad
mov ebp, esp
mov edx, [ebp+40]
call writeString
call CrLf
mov edx, [ebp+36]
call writeString
call CrLf
popad
ret 8
Intro ENDP
;-------------------------------------------------------
;Prompts user for an integer, int stores in userNum
; Receives parameters on the system stack (in the order pushed):
; Address of the error message
; Address of the prompt
; Address of return value
;Post: userNum
;registers: none
;-------------------------------------------------------
GetData PROC
pushad
;setup stack and prompt for entry
mov ebp, esp
reenter:
mov edx, [ebp+40]
mov ebx, [ebp+36]
call WriteString
call ReadInt
;validate entry
cmp eax, MIN ;if eax < LOWER
jl badEntry ;jump to summary
cmp eax, MAX ;if eax > UPPER
jg badEntry ;reprompt
jmp goodEntry ;else jump to end, we have a good value
;bad entry reprompt
badEntry:
mov edx, [ebp+44]
call WriteString
jmp reenter
goodEntry:
call CrLf
mov [ebx], eax
popad
ret 12
GetData ENDP
;-------------------------------------------------------
;Fills array with a number of random integers within RANGE
;Recieves parameters on the system stack (in order pushed)
; array
; userNum
;Post: array is filled with userNum number of randoms
;Registers used: none
;-------------------------------------------------------
FillArray PROC
pushad
mov ebp, esp
mov ecx, [ebp+36] ;initialize loop counter with user entry
mov edi, [ebp+40] ;setup array offset
fillLoop:
call nextRand
add edi, 4
loop fillLoop
popad
ret 8
FillArray ENDP
;-------------------------------------------------------
; Procedure nextRand
; adapted from check lecture 20 solutions
; Procedure to get the next random number in the range specified by the user.
; Preconditions: LRANGE < HRANGE
; Registers used: eax, edi
;-------------------------------------------------------
nextRand PROC
mov eax, HRANGE
sub eax, LRANGE
inc eax ;add 1 to get the number of integers in range
call RandomRange
add eax, LRANGE ;eax has value in [LOW - HIGH]
mov [edi],eax
ret
nextRand ENDP
;-------------------------------------------------------
;Sorts the contents of an integer array
; Receives parameters on the system stack (in order pushed)
; Array
; Array Size
;registers: none
;-------------------------------------------------------
sortList PROC
pushad
mov ebp, esp
mov ecx, [ebp+36]
mov edi, [ebp+40]
dec ecx ;ecx < request-1
mov ebx, 0 ;ebx=k
;for(k=0; k<request-1; k++)
outerLoop:
mov eax, ebx ;eax=i=k
mov edx, eax
inc edx ;edx=j=k+1
push ecx
mov ecx, [ebp+36] ;ecx < request
;for(j=k+1; j<request; j++)
innerLoop:
mov esi, [edi+edx*4]
cmp esi, [edi+eax*4]
jle skip
mov eax, edx
skip:
inc edx
loop innerLoop
;swap elements
lea esi, [edi+ebx*4]
push esi
lea esi, [edi+eax*4]
push esi
call exchange
pop ecx
inc ebx
loop outerLoop
popad
ret 8
sortList ENDP
;-------------------------------------------------------
; Exchange k and i
; Receives parameters on the system stack (in order pushed)
; array[k]
; array[i]
;registers: none
;-------------------------------------------------------
Exchange PROC
pushad
mov ebp,esp
mov eax, [ebp+40] ;array[k] low number
mov ecx, [eax]
mov ebx, [ebp+36] ;array[i] high number
mov edx, [ebx]
mov [eax], edx
mov [ebx], ecx
popad
ret 8
Exchange ENDP
;-------------------------------------------------------
;Displays the median of an integer array
; Receives parameters on the system stack (in order pushed)
; Array
; Array Size
; display string
;registers: none
;-------------------------------------------------------
Median PROC
pushad
mov ebp, esp
mov edi, [ebp+44]
;display string
mov edx, [ebp+36]
call writeString
;calculate median element
mov eax, [ebp+40]
cdq
mov ebx, 2
div ebx
shl eax, 2
add edi, eax
cmp edx, 0
je isEven
;Array size is odd, so display the middle value
mov eax, [edi]
call writeDec
call CrLf
call CrLf
jmp endMedian
isEven:
;Array size is even so average the two middle values
mov eax, [edi]
add eax, [edi-4]
cdq
mov ebx, 2
div ebx
call WriteDec
call CrLf
call CrLf
endMedian:
popad
ret 12
Median ENDP
;-------------------------------------------------------
;Displays the contents of an integer array, 10 per row
; Receives parameters on the system stack (in order pushed)
; Array
; Array Size
; display string
;registers: none
;-------------------------------------------------------
DisplayList PROC
pushad
mov ebp, esp
;display string
mov edx, [ebp+36]
call writeString
call CrLf
mov ecx, [ebp+40]
mov edi, [ebp+44]
mov ebx, 0
;display array contents
listloop:
inc ebx ;counter for 10 items per row
mov eax, [edi]
call writeDec
add edi, 4
cmp ebx, 10
jne noReturn ;jump if 10 items are not yet printed
call CrLf
mov ebx, 0
jmp noTab ;this skips adding a tab on a new row
noReturn:
mov al, TAB
call writeChar
noTab:
loop listloop
call CrLf
popad
ret 12
DisplayList ENDP
;-------------------------------------------------------
;Says good-bye to the user
; Receives parameters on the system stack:
; Address of string
;registers: none
;-------------------------------------------------------
Goodbye PROC
pushad
mov ebp, esp
mov edx, [ebp+36]
call writeString
call CrLf
popad
ret 4
Goodbye ENDP
END main
In order to exchange elements you should be passing their address (in other words, a pointer to each). What you did was just swapping the values passed as parameters, which were also immediately freed. Your code is the equivalent of this C function:
void Exchange(int x, int y)
{
int eax = x;
int ebx = y;
x = ebx;
y = eax;
}
You need something like:
void Exchange(int* x, int* y)
{
int eax = *x;
int ebx = *y;
*x = ebx;
*y = eax;
}
In asm, that may look like:
Exchange PROC
pushad
mov eax, [esp+40]
mov ecx, [eax]
mov ebx, [esp+36]
mov edx, [ebx]
mov [eax], edx
mov [ebx], ecx
popad
ret 8
Exchange ENDP
To call this function you would use the following:
lea esi, [edi+ebx*4]
push esi
lea esi, [edi+eax*4]
push esi
call Exchange
Note your Exchange function had an instruction mov eax, [edi] which I couldn't make sense of.
Update: Yes, you can emulate the functionality of LEA by doing the calculations manually. For example, lea esi, [edi+ebx*4] becomes:
mov esi, ebx
shl esi, 2 ; esi=ebx*4
add esi, edi ; esi=edi+ebx*4

Resources