2018/11/11 CS 2XA3 (2018/19, Term I) Major Lab 2
CS 2XA3 (2018/19, Term I) Final project — lab section L01
Back To Lab Menu Back To Main Menu Submissions Log Out
There is only one task and one deliverable: NASM program sorthem.asm. It can be submitted either via the course website, or using 2xa3submit script from . For 2xa3submit submission please use 2xa3submit AAA xproj1 sorthem.asmwhereAAAisyourstudentnumber.Youcansubmitthefileas many times as you wish, the latest submission will be used for marking. The submission is opened from 8:30 on Nov 2, 2018 and will close down at 23:00 on Dec. 6, 2018 (there will be no extension possible). If submission is not possible for whatever reason (typically right after the submission closes), email the file as an attachment immediately (needed for the time verification) to Prof. Franek (franek@mcmaster.ca) with an explanation of the problem; you must use your official McMaster email account for that. Include the course code, your lab section, full name, and your student number. Note that if there is no problem with submission (typically the student using a wrong name for the file), you might be assessed a penalty for email submission, depending on the reason you used email.
Before you start you need to download the appropriate supporting files
http://www.cas.mcmaster.ca/~franek/courses/cs2xa3/labs/lab.cgi?lab
1/6
eroom
. ro t c u r ts n i e h t r o s ‘ AT e h t h t i w s n o i t a t l u s n o c e r a s n o i t p e c x E . n g i s e d r o n o i t u l o s e h t n o e l po e p r e h t o h ti w e t a r e p oo c o t d e w o l l a t o n e r a u o y s u h t d n a , t c e j o r p l a u d i v i d n i n a s i t c e j o r p l a n i F
2018/11/11 CS 2XA3 (2018/19, Term I) Major Lab 2
asm_io.asm
asm_io.inc
cdecl.h
driver.c (careful, this is a slightly different driver.c than used for labs).
NASM program named sorthem.asm
The name of your file must be sorthem.asm and below is a description of what it should do when
executed
1. sorthem.asm expects one command line argument. It must be an unsigned integer bigger or equal to 2 and less or equal to 9. If there are more or fewer command line arguments than one, the program displays an error message and terminates. If the single command line argument does not evaluate to an integer between 2 and 9, an error message is displayed and the program terminates.
- The program uses an integer array of length 9 to represent a peg. Each item of the array represents the size of the disk at that position on the disk.
Thus, [4,3,1,2,0,0,0,0,0] represents a configurationoo|oo o|o ooo|ooo oooo|oooo XXXXXXXXXXXXXXXXXXXXXXX
while [5,4,3,2,1,0,0,0,0] represents a configuration
o|o oo|oo ooo|ooo oooo|oooo ooooo|ooooo
XXXXXXXXXXXXXXXXXXXXXXX
- Then single command line argument of the value between 2 and 9 represents a number of disks on the peg.
http://www.cas.mcmaster.ca/~franek/courses/cs2xa3/labs/lab.cgi?lab
2/6
.
2018/11/11 CS 2XA3 (2018/19, Term I) Major Lab 2
- The program calls a subprogram rconf that creates a random initial configuration. This program is prepared for you in the driver.c , so you do not need to worry about. The important thing is to call it properly: first you need to push on the stack the number of disks, then the address of the array representing the peg.
- The program displays the initial peg configuration as created by rconf and pauses and waits for the user to depress a key.
- The display of the peg configuration is done by a subprogram showp that you have to write. The subprogram showp expects two parameters on the stack; on top of the stack the address of the array representing the peg, and below the number of disks. Before showp returns, it calls read_char so the program is paused and waits for the user to depress a key.
- Then the initial configuration is passed to a subprogram sorthem that you have to write. The subprogram sorthem expects two parameters on the stack, at the top the address of the peg and below the number of disks.
- The subprogram sorthem actually sorts the disks on the peg in an ascending order. This is achieved by recursion. It first makes a recursive call to sorthem and passes it the peg array starting from the second item, not the first and the number of disks decreased by one.
ebx ebx+4 ecx ecx-1 .
- So after the recursive call, the tail of the peg (except the very first item) is sorted. All you need to do is now to find where to put the very first item if the array representing the peg. This is done by swapping the 1st item with the 2nd if the first item is smaller than the 2nd, then swapping the 2nd item with the 3rd, if the 2nd item is smaller that the 3rd, end so on, as long as the swap takes place (see below for example). When the swapping is done, if the peg configuration was modified (i.e. a swap took place), the modified configuration is displayed using showp .
- When done, the final configuration is displayed again using showp .
- After displaying the final configuration, the program terminates.
http://www.cas.mcmaster.ca/~franek/courses/cs2xa3/labs/lab.cgi?lab
3/6
rebmunehtllacevisrucerehtotssaplliwti,sksidforebmun ehtsniatnocfidnasserddaehtllacevisrucerehtotssaplliwti,nidlehsisserdda
yarragepehtfi:tniH
2018/11/11 CS 2XA3 (2018/19, Term I) Major Lab 2
Asamplerunwith4disks(sorthem 4) initial configuration
ooooo|ooooo ooo|ooo oooo|oooo oo|oo XXXXXXXXXXXXXXXXXXXXXXX
ooooo|ooooo ooo|ooo oo|oo oooo|oooo XXXXXXXXXXXXXXXXXXXXXXX
ooooo|ooooo oo|oo ooo|ooo oooo|oooo XXXXXXXXXXXXXXXXXXXXXXX
oo|oo ooo|ooo oooo|oooo ooooo|ooooo XXXXXXXXXXXXXXXXXXXXXXX
final configuration
oo|oo ooo|ooo oooo|oooo ooooo|ooooo XXXXXXXXXXXXXXXXXXXXXXX
Asamplerunwith5disks(sorthem 5) initial configuration
ooo|ooo
http://www.cas.mcmaster.ca/~franek/courses/cs2xa3/labs/lab.cgi?lab
4/6
2018/11/11 CS 2XA3 (2018/19, Term I) Major Lab 2
oooo|oooo ooooo|ooooo oooooo|oooooo oo|oo XXXXXXXXXXXXXXXXXXXXXXX
ooo|ooo oooo|oooo ooooo|ooooo oo|oo oooooo|oooooo XXXXXXXXXXXXXXXXXXXXXXX
ooo|ooo oooo|oooo oo|oo ooooo|ooooo oooooo|oooooo XXXXXXXXXXXXXXXXXXXXXXX
ooo|ooo oo|oo oooo|oooo ooooo|ooooo oooooo|oooooo XXXXXXXXXXXXXXXXXXXXXXX
oo|oo ooo|ooo oooo|oooo ooooo|ooooo oooooo|oooooo XXXXXXXXXXXXXXXXXXXXXXX
final configuration
oo|oo ooo|ooo oooo|oooo ooooo|ooooo oooooo|oooooo XXXXXXXXXXXXXXXXXXXXXXX
http://www.cas.mcmaster.ca/~franek/courses/cs2xa3/labs/lab.cgi?lab
5/6
2018/11/11 CS 2XA3 (2018/19, Term I) Major Lab 2
Example of sorthem work:
Imagine that the array represening the peg to be sorted as passed to sorthem is 1,2,4,3,0,0,0,0,0 and number of disks is 4. The subprogram sorthem will make a recursive call to sorthem and passes it peg 2,4,3,0,0,0,0,0 and number of disks 3. After the recursive call completion, the passed array is sorted, i.e. 4,3,2,0,0,0,0,0, i.e. the whole array is now 1,4,3,2,0,0,0,0,0. Now 1 must be put in its proper place by swapping: 1 is swapped with 4 getting 4,1,3,2,0,0,0,0,0, then 1 is swapped with 3 getting 4,3,1,2,0,0,0,0,0, then 1 is swapped with 2 getting 4,3,2,1,0,0,0,0,0, and then the swapping ends as 1 is bigger than 0. Since some swapping took place, the new configuration 4,3,2,1,0,0,0,0,0 is displayed.
http://www.cas.mcmaster.ca/~franek/courses/cs2xa3/labs/lab.cgi?lab
6/6