124k views
5 votes
Write an x86_64 Assembly program that sorts a programmer-defined array. The size of the array and the address of the array should be passed to the called sort function via registers. You can choose the sorting algorithm to be implemented. A simple example would be Bubble Sort. Test using 5 or 10 elements. You know know how to print character digits, you may print the array before and/or after sorting to look at the contents of your array.

User Villasv
by
5.1k points

1 Answer

4 votes

Answer:

Check the explanation

Step-by-step explanation:

Algorithm –

Load data from offset 500 to register CL (for count).

Travel from opening memory position to last and evaluate the two numbers if first number is bigger than second number then we will have to swap them.

First pass fix the position for last number.

Decrease the count by 1.

Again travel from opening memory position to (last-1, by help of count) and compare 2 numbers if second number is smaller than that of the first number then swap them.

Second pass fix the position for last two numbers.

Repeate.

Step by step Explanation –

MOV SI, 500: set the value of SI to 500.

MOV CL, [SI]: load data from offset SI to register CL.

DEC CL: decrease value of register CL BY 1.

MOV SI, 500: set the value of SI to 500.

MOV CH, [SI]: load data from offset SI to register CH.

DEC CH: decrease value of register CH BY 1.

INC SI: increase value of SI BY 1.

MOV AL, [SI]: load value from offset SI to register AL.

INC SI: increase value of SI BY 1.

CMP AL, [SI]: compares value of register AL and [SI] (AL-[SI]).

JC 41C: jump to address 41C if carry generated.

XCHG AL, [SI]: exchange the contents of register AL and SI.

DEC SI: decrease value of SI by 1.

XCHG AL, [SI]: exchange the contents of register AL and SI.

INC SI: increase value of SI by 1.

DEC CH: decrease value of register CH by 1.

JNZ 40F: jump to address 40F if zero flat reset.

DEC CL: decrease value of register CL by 1.

JNZ 407: jump to address 407 if zero flat reset.

HLT: stop.

User David Castro
by
5.9k points