Assignment 1: Submission, Script, Optimization
Submission
You are expected to submit FOUR files:
- assign1a.s (or .asm 1)
- assign1b.asm (or .m4 1)
- script1.txt
- script2.txt
I will assemble the executables from your assembly files. If you submit executable files, I will not look at them.
1Note: There is no significant difference between .s and .asm files, so I don't care if you name them .s or .asm. Compilers usually output .s files, so programmers often use .asm to denote handwritten code. An advantage of doing this is if you accidentally compile a .asm file and output .s, you won't overwrite your handwritten code.
In lectures, you were taught to include M4 macros in your handwritten .asm file, and then use m4 myprog.asm > myprog.s
. This is why the assignment says you should submit a .asm file for your optimized version. In tutorials, I said to save your handwritten file (with macros) as .m4, and then use m4 myprog.m4 > myprog.s
, so you can submit .m4 for the optimized version if you'd like.
Script
Using script is very simple.
script script1.txt // start script logging, saving or outputting the log to a file called script1.txt
// everything you do here will be logged (saved) to script1.txt
gcc yourprogram.s -o yourprogram
gdb yourprogram // start gdb with your program loaded
display/i $pc // display the current instruction at every break
b topofloop // break at the start of your loop, so you can step through the loop one instruction at a time
r // run/execute your program, it will pause at topofloop (your label name might differ)
ni // start single stepping through the loop...
p/d $x19 // print all the important registers (ie. in this case, your y value which is being modified inside the loop)
ni // print the register a few times as you step through your program with ni, to show it being modified
ni
... // ...til end of loop (at least one complete iteration of your loop for version 1)
c // continue until end of program, or delete to clear your breakpoints and then continue
q // quit gdb
exit // exit script, stop logging
Open your log in an editor like vim or nano and check that everything was indeed logged 2.
2 Note: You will notice either weird block symbols in your script, or many extra characters like ^H or ^K. These are control characters, and you can leave them in your script file. I will ignore them as long as you have logged everything the assignment asked you to.
For version 2, the easiest way would be to add a label like "finalresult:" to the line where your minimum is calculated. Then just add a breakpoint at finalresult (b finalresult), do a single step (si), and print out the value of your minimum (p/d $<minimum_register>, eg. p/d $x19).
Optimization
- Put the loop test at the bottom of the loop (). Refer to my earlier example, compare the two versions.
- Use the madd instruction to combine multiply and add instructions.
format: madd Xd, Xn, Xm, Xa
Xd = (Xn * Xm) + Xa
example: madd x20, x21, x22, x23
x20 = (x21 * x22) + x23
- Use macros to make your code more readable. Heavily used registers should be replaced with macros (eg. x_r, y_r, loop_r etc.).
Refer to the optimized loop example to see how macros are defined.