Wednesday, October 2, 2019

SPO600 Lab3 Pt1/2

We started working on assembly as part of our lab, for the first part we worked with Xerxes (X86_64)

This was fun and much harder than the programming I've done for most of my life. In modern programming languages we would use variables to hold values or even refer to other variables. However in assembly, there are no variables in a sense. I can't just say int i = 1, if I want to use a loop I can't just keep increasing i and print it. 

I have to use the available registers and assign a value to them either from another register denoted by % or an actual value denoted by $. Another thing to take note of when writing in assembly is there are two 1 in a sense, 1 as a value... and 1 as a written number. In assembly, we found out the hard way if we tried to to output the current count of the loop in register 14, we wouldn't get a number. It is because we didn't change it to ASCII, if we printed our loop we'd see nothing really following Loop: , because the first 9 ASCII characters in the table are not even numbers. To get "string" output of the number we had to add a value of 48 to the current loop which you'll notice with the line 

add    $48, %r14

after that we replace the character c we put in the message as a placeholder with the number in register 14 to get the output. As a group we messed up earlier because we didn't change the line 

.section.rodata

and started getting compiler issues. It was because ro meant the line was read-only and we were trying to write to it. By changing the line to 

.section.data

the issue was solved. The syscall line will then output the msg line with the correct loop #, the register keeping track of the current loop count will then increase by 1 and we then compare if the loop count is equal to max(10). If not we jump back to the start of the loop: section and repeat.

When the current loop count is equal to max and comparison is done, a flag will not be triggered allowing the program to bypass the jne loop and move onto the movq $0, %rdi line


The next loop we wrote had to show a count of 00-30 while suppressing leading 0, meaning the program shouldn't write 01, 02 and so forth. A few immediate changes we made was to increase max to 30(duh) and add one more space preceding the c to account for the extra character. 

The difference here is that we use a divisor of 10 so we don't run into issues when replacing a character in the message with a number. Also the div operation always uses two registers, rax for quotient and rdx for the remainder. Another rule is rdx has to always be zeroed before the next division happens hence the 

mov $0, %rdx 

at the start of the loop: section. One other thing to note is the addition of two sections more: and less:. Which is really a if or else condition. Based on the the comparison between the current loop count and 10 one or both sections will be triggered. 




No comments:

Post a Comment

Contains Duplicate (Leetcode)

I wrote a post  roughly 2/3 years ago regarding data structures and algorithms. I thought I'd follow up with some questions I'd come...