SPO600 Lab 3 Assembly Language

This has been a fun experience writing in assembly language. I have always been interested in seeing how assembly is written and how it operates. This lab has helped put a lot of insight into how assembly works and the benefits of writing in it.

The task involved creating a simple program that is able to print text and number in a loop, like this :
output

The program basically start at 0 and loops until 30, printing “Loop: + Current Iteration Number”. To achieve this result the following was the program made in x86 assembly.

x86code.PNG

Now I will break the code down into parts and try to explain to best if my ability what the code is doing.

start

This is the initialization part of the program. We set our loop range with start and max, and insert the value of start into registry r15. We also add 0x30 to registry r12, this is the ascii value of 0 in hex, we will use this value later on in order to check if our quotient is 0, in order to get rid of leading 0s when printing.

loop.PNG

Now here is the juicy part, containing almost all of the logic behind the program. Starting of we have a mov command to clear out rdx registry, this is needed in order for div to work properly with remainders. After that we have our division logic, simply storing values and calling div. Note we store both quotient and remainder, since we will need both of them in order to print double digit values. We then convert quotient and remainder to ascii and store remainder at msg position 7. In order to suppress leading 0 we check to see if quotient is 0 or not, that is where we use the ascii 0 value. If it is 0 we skip adding it into the msg, however if it is not o we add it right before remainder at msg position 6.

print.PNG

Now that we have our msg modified we are good to go on printing it on to the screen. Simply adding stdout parameters into necessary registers and calling syscall, we are able to print 1 iteration of the msg onto the console. After the print is finished we have to do a loop check, think of it as do() while() . We simply increment the counter and compare it to the maximum number of times we want to run, if it does not match we return back to the loop tag and start the process again.

exit

Finally we have the exit command, and our ending .data which holds the string we used to add the counter to and the length of the msg itself for stdout parameter.

That’s it for the programs logic, now let’s quickly go over the small difference in the aarch64 example of the same program. Here is the code used to run the same program in aarch64 environment :

aarch64.PNG

I will not go in explaining the program, as it is almost exactly the same logic as the on in x86 assembly. I will however point out some of the difference between the two assemblers. One big difference is syntax, in x86 we use value->registry, while in aarch its registry<-value. There are some difference in the commands and registries, for example we can use 30 registers compared to 15 in x86, making it nicer for bigger programs that do not want to start using memory. Honestly the difference is mostly in the way syntax works, the logic behind the assembly language is the same in both architectures. However I personally found it a bit easier to type in x86, but I think it was because I did most of my examples using it.

Thanks for reading the post, hope it way useful in one way or another.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s