Skip to main content

1. Sort in Assembly

Late submissions will not be accepted. Please complete and submit your assignment well before the deadline.

Goals​

  • Understand how the stack works
  • Understand x86 calling conventions
  • Learn how to write x86 assemblies

Preparation​

Download the handout tarball, assign-1-handout.tar.gz and then decompress it. The following commands show how you download the tarball using wget and then decompress it using the command tar.

$ wget https://compsec.snu.ac.kr/class/systems-programming/files/assign-1-handout.tar.gz

...

$ tar -xvf ./assign-1-handout.tar.gz
assign-1-handout/
assign-1-handout/Makefile
assign-1-handout/prepare-submit.sh
assign-1-handout/sort_tree.s
assign-1-handout/main.c

$ cd assign-1-handout

$ ls -1
Makefile
main.c
prepare-submit.sh
sort_tree.s

Problem description​

The program, main is implemented with main.c and sort_tree.s. Once executed, main first picks 10 random values (see gen_rand_nums()). Then it constructs and then populates the binary search tree with those 10 random values (see make_tree()).

Your job is to implement sort_tree(), which traverses the tree (where its root is provided with the parameter, root) to sort all tree elements into the array sorted_nums[]. Of course we don't ask you to implement this sort_tree() in C. You must implement your sort_tree() using x86 assembly, which should be written in the file sort_tree.s. To be more specific, the below shows the provided sort_tree.s, and your job is to write your own assemblies where TODO comments are placed.

sort_tree.s
	.text
.globl sort_tree
.type sort_tree, @function
sort_tree:
pushq %rbp
movq %rsp, %rbp

/* TODO: Write your code! */

leave
ret
.size sort_tree, .-sort_tree
.section .note.GNU-stack,"",@progbits

The reference solution implements the in-order tree traverser algorithm with around 40 lines of assembly code. You don't need to follow the reference implementation though.

Specification​

  • The tree always has 10 nodes, and their values are distinct integers from 0 to 99 (inclusive). The testcases used for grading also follow this.
  • main() calls sort_tree(root, 0, sorted_nums). The second argument, i, and the return value of sort_tree() are not checked, so you can use them freely. (The reference solution uses i for indexing sorted_nums[].)
  • There is no restriction on how you implement sort_tree() (e.g., which registers to use, or 32-bit vs. 64-bit registers), as long as it sorts the tree elements correctly.
tip

If it's unclear which logics should be implemented within sort_tree(), you can refer to check_if_sorted(). This function checks if sorted_nums[] holds all elements of nums[] in ascending order.

warning

In this problem, you are only allowed to modify sort_tree.s. As such, you are NOT allowed to modify any other files.

Run and test your implementation​

After implementing your sort_tree.s, you can run and test as follows.

$ ls
Makefile main.c prepare-submit.sh sort_tree.s

$ make
gcc -fno-asynchronous-unwind-tables -fcf-protection=none -c main.c -o main.o
gcc -fno-asynchronous-unwind-tables -fcf-protection=none -c sort_tree.s -o sort_tree.o
gcc main.o sort_tree.o -o main

$ ls
Makefile main main.c main.o prepare-submit.sh sort_tree.o sort_tree.s

$ ./main
[+] PASS

If you see the output [+] PASS by executing main, then your implementation is likely correct. Make sure that the executable main is generated based on your sort_tree.s implementation.

main picks random values based on the current time. You can also give 10 values in the order they are inserted into the tree, and make test runs main with a few sample inputs.

$ ./main 50 25 75 12 37 62 87 6 18 31
[+] PASS

$ make test
./main 50 25 75 12 37 62 87 6 18 31
[+] PASS
./main 0 1 2 3 4 5 6 7 8 9
[+] PASS
./main 99 98 97 96 95 94 93 92 91 90
[+] PASS
warning

Note that it is not guaranteed that your submission will receive the full scores simply because you meet the test condition above. We use more testcases in the evaluation, which further checks if your implementation correctly follows the problem specification. Since we won't provide such extended testcases to you, it's your job to thoroughly check if your implementation is correctly done.

Submission​

Prepare your submission by executing the script, prepare-submit.sh. It creates a compressed tar ball, assign-1-submit.tar.gz using your ./sort_tree.s.

$ bash ./prepare-submit.sh
[*] Remove tar file...
[*] Compress files...
./sort_tree.s
[*] Successfully Compressed!
[*] Done! You are ready to submit: assign-1-submit.tar.gz

Then, transfer the assign-1-submit.tar.gz file from your container on the sivir server to your local computer using the scp command:

# Open your local terminal (e.g., PowerShell, Bash, etc.)
$ scp -P <port> compsec@sivir.snu.ac.kr:~/assign-1-handout/assign-1-submit.tar.gz ./
compsec@sivir.snu.ac.kr's password:
assign-1-submit.tar.gz 100% 273 36.8KB/s 00:00

$ ls
assign-1-submit.tar.gz
  • <port>: This is the SSH port used to connect to the container.

Finally, once you have the assign-1-submit.tar.gz file on your local machine, upload it to the eTL. You don't need to rename the file.

Honor Code​

You may discuss ideas with your classmates or through eTL, and you may use AI tools. However, do NOT share your code with others, and do NOT submit someone else's code, including the code of students who took this course before.

warning

We check all submissions, including those from the previous course offerings, with a plagiarism checker. If you get caught, you will get F and be reported to the student committee in the department. Sharing your code gets the same penalty as copying it.