User Tools

Site Tools


spo600:64-bit_assembly_language_lab

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
spo600:64-bit_assembly_language_lab [2024/02/06 15:11] chrisspo600:64-bit_assembly_language_lab [2025/02/11 12:26] (current) chris
Line 5: Line 5:
 Perform this lab on [[SPO600 Servers]] (you may use your own systems if they are of the right architecture and appropriately configured). Perform this lab on [[SPO600 Servers]] (you may use your own systems if they are of the right architecture and appropriately configured).
  
-=====  Lab  =====+=====  Lab  =====
  
 ====  Code Examples  ==== ====  Code Examples  ====
Line 43: Line 43:
 ====  Optional Investigation  ==== ====  Optional Investigation  ====
  
-These steps were performed in the lecture class.+**These steps were performed in the lecture class.**
  
-1. Build and run the three versions of the program for x86_64 and aarch64, using ''make''. Take a look at the differences in the code.+1. Build and run the C version(s) of the program for x86_64 and aarch64, using ''make''. Take a look at the differences in the code.
  
 2. Use the ''objdump -d'' command to dump (print) the object code (machine code) and disassemble it into assembler for each of the binaries. Find the ''<nowiki><main></nowiki>'' section and take a look at the code. Also notice the total amount of code. 2. Use the ''objdump -d'' command to dump (print) the object code (machine code) and disassemble it into assembler for each of the binaries. Find the ''<nowiki><main></nowiki>'' section and take a look at the code. Also notice the total amount of code.
  
-3. Review, build, and run the x86_64 assembly language programs using ''make'', taking note of the commands that are executed to assemble and link the code. Take a look at the code using ''objdump -d **objectfile**'' and compare it to the source code. Notice the absence of other code (compared to the C binary, which had a lot of extra code).+3. Compare the disassembled output from step 2 above with the assembly language output of the C compiler, produced by running ''gcc'' with the ''-S'' option.
  
-4Build and run the assembly language version of the program for aarch64 using ''make'', taking note of the commands that are executed to assemble and link the code. Verify that you can disassemble the object code in the ELF binary using ''objdump -d //objectfile//'' and take a look at the code.+3Review, build, and and run the aarch64 assembly language program (on the [[SPO600 Servers|aarch64 class server]]) using ''make'', taking note of the commands that are executed to assemble and link the code. Verify that you can disassemble the object code in the ELF binary using ''objdump -d //objectfile//'' and take a look at the code.
  
-====  Lab Tasks  ====+4. Review, build, and run the x86_64 assembly language programs using ''make'' (on the [[SPO600 Servers|x86 class server]]), taking note of the commands that are executed to assemble and link the code. Take a look at the code using ''objdump -d **objectfile**'' and compare it to the source code. Notice the absence of other code (compared to the C binary, which had a lot of extra code). Note also that there are two versions of the code, one written for the NASM assembler and one written for the GNU as assembler (aka "gas"). In this course we will be focused on gas.
  
-Steps 1-were demonstrated in the lecture class. Repeat those steps and then build on them using steps 4-6.+ 
 +====  AArch64 (Complete this part in your in-class breakout group, if possible)  ==== 
 + 
 +The steps in the previous section were demonstrated in the class.  
 + 
 +Perform these steps in your breakout group using the "mob programming" approach used in Lab 1: 
 + 
 +0. Have the group driver share their screen, log into the [[SPO600 Servers|aarch64 server]], and unpack the archive (above).
  
 1. Review, build, and run the aarch64 assembly language programs. Take a look at the code using ''objdump -d //objectfile//'' and compare it to the source code. 1. Review, build, and run the aarch64 assembly language programs. Take a look at the code using ''objdump -d //objectfile//'' and compare it to the source code.
  
-2. Here is a basic loop in AArch64 assembler - this loops from 0 to 9, using r19 as the index (loop control) counter:+2. Here is a basic loop in AArch64 assembler - this loops from 0 to 5, using r19 as the index (loop control) counter:
  
 <code> <code>
Line 65: Line 72:
  .globl _start  .globl _start
  min = 0                          /* starting value for the loop index; **note that this is a symbol (constant)**, not a variable */  min = 0                          /* starting value for the loop index; **note that this is a symbol (constant)**, not a variable */
- max = 10                         /* loop exits when the index hits this number (loop condition is i<max) */+ max =                         /* loop exits when the index hits this number (loop condition is i<max) */
  _start:  _start:
      mov     x19, min      mov     x19, min
  loop:  loop:
 +
      /* ... body of the loop ... do something useful here ... */      /* ... body of the loop ... do something useful here ... */
-     add     x19, x19, 1 + 
-     cmp     x19, max +     add     x19, x19, 1     /* increment the loop counter */ 
-     b.ne    loop +     cmp     x19, max        /* see if we've hit the max */ 
-     mov     x0, 0           /* status -> 0 */+     b.ne    loop            /* if not, then continue the loop */ 
 +      
 +     mov     x0, 0           /set exit status to 0 */
      mov     x8, 93          /* exit is syscall #93 */      mov     x8, 93          /* exit is syscall #93 */
      svc                   /* invoke syscall */</code>      svc                   /* invoke syscall */</code>
Line 80: Line 90:
  
 <code> <code>
- Loop 
- Loop 
- Loop 
- Loop 
  Loop  Loop
  Loop  Loop
Line 99: Line 105:
  Loop: 3  Loop: 3
  Loop: 4  Loop: 4
- Loop: 5 + Loop: 5</code>
- Loop: 6 +
- Loop: 7 +
- Loop: 8 +
- Loop: 9</code>+
  
 Character conversion tip: In order to print the loop index value, you will need to convert from an integer to digit character. In ASCII/ISO-8859-1/Unicode UTF-8, the digit characters are in the range 48-57 (0x30-0x39). You will also need to assemble the message to be printed for each line - you can do this by writing the digit into the message buffer before outputting it to stdout, which is probably the best approach, or you can perform a sequence of writes for the thee parts of the message ('Loop: ', number, '\n'). You may want to refer to the manpage for ''ascii''. Character conversion tip: In order to print the loop index value, you will need to convert from an integer to digit character. In ASCII/ISO-8859-1/Unicode UTF-8, the digit characters are in the range 48-57 (0x30-0x39). You will also need to assemble the message to be printed for each line - you can do this by writing the digit into the message buffer before outputting it to stdout, which is probably the best approach, or you can perform a sequence of writes for the thee parts of the message ('Loop: ', number, '\n'). You may want to refer to the manpage for ''ascii''.
Line 109: Line 111:
 For reference, here is a [[6502 Counting Loop Example|6502 implementation of this loop]]. For reference, here is a [[6502 Counting Loop Example|6502 implementation of this loop]].
  
-3. Repeat the previous step for x86_64.+3. Extend the AArch64 code to loop from 00-32, printing each value as a 2-digit decimal number. 
 + 
 +2-Digit Conversion tip: You will need to take the loop index and convert it into separate digits by dividing by 10; the quotient will be the first digit and the remainder will be the second digit. For example, if the loop index value is 25 in decimal, dividing by 10 will yield a quotient of 2 and a remainder of 5. You will then need to convert each digit into a character (using the same approach used in the single-digit loop). Read the description of the division instruction carefully. On x86_64, you need to set up specific registers before performing a division. On AArch64, you will need to use a second instruction to find the remainder after a division. 
 + 
 +4. Change the code as needed to suppress the leading zero (printing 0-32 instead of 00-32). To do this, you'll need to add a conditional into your code (the equivalent of an "if" statement, implemented as a compare followed by a conditional branch or conditional jump). 
 + 
 +5. Make a copy of the code and change it to output in hexadecimal (0-20) instead of decimal (0-32). 
 + 
 +==== x86 (Complete this part after class) ==== 
 + 
 +5. Repeat the previous steps for x86_64.
  
 For reference, here is the loop code in x86_64 assembler: For reference, here is the loop code in x86_64 assembler:
Line 117: Line 129:
  .globl    _start  .globl    _start
  min = 0                         /* starting value for the loop index; **note that this is a symbol (constant)**, not a variable */  min = 0                         /* starting value for the loop index; **note that this is a symbol (constant)**, not a variable */
- max = 10                        /* loop exits when the index hits this number (loop condition is i<max) */+ max =                        /* loop exits when the index hits this number (loop condition is i<max) */
  _start:  _start:
      mov     $min,%r15           /* loop index */      mov     $min,%r15           /* loop index */
  loop:  loop:
      /* ... body of the loop ... do something useful here ... */      /* ... body of the loop ... do something useful here ... */
-     inc     %r15                /* increment index */ +     inc     %r15                /* increment the loop index */ 
-     cmp     $max,%r15           /* see if we're done */ +     cmp     $max,%r15           /* see if we've hit the max */ 
-     jne     loop                /* loop if we're not */ +     jne     loop                /* if not, then continue the loop */ 
-     mov     $0,%rdi             /* exit status */ +      
-     mov     $60,%rax            /* syscall sys_exit */ +     mov     $0,%rdi             /set exit status to 0 */ 
-     syscall</code> +     mov     $60,%rax            /* exit is syscall #60  */ 
- +     syscall                     /* invoke syscall */</code>
-4. Extend the AArch64 code to loop from 00-30, printing each value as a 2-digit decimal number. +
- +
-2-Digit Conversion tip: You will need to take the loop index and convert it to a 2-digit decimal number by dividing by 10. Read the description of the division instruction carefully. On x86_64, you need to set up specific registers before performing a division. On AArch64, you will need to use a second instruction to find the remainder after a division.+
  
-5. Change the code as needed to suppress the leading zero (printing 0-30 instead of 00-30). 
  
-6Repeat the previous two steps for x86_64.+**Note:** The x86 division instruction is quite a bit different from the aarch64 division instructionsRead the documentation for the instructions carefully (either in the quick start notes or the reference manual).
  
 ====  Deliverables  ==== ====  Deliverables  ====
  
-1. Complete the lab section, above.+1. Complete the lab sections, above.
  
 2. Blog about the programs you've written. Describe the experience of writing and debugging in assembler, as compared to writing in other languages. Contrast 6502, x86_64, aarch64 assembler, your experience with each, and your opinions of each. Include links to the source code for each of your assembler programs (Important: wherever possible, use text instead of screenshots, so that your code can be indexed, searched, and tested). 2. Blog about the programs you've written. Describe the experience of writing and debugging in assembler, as compared to writing in other languages. Contrast 6502, x86_64, aarch64 assembler, your experience with each, and your opinions of each. Include links to the source code for each of your assembler programs (Important: wherever possible, use text instead of screenshots, so that your code can be indexed, searched, and tested).
Line 168: Line 176:
   4 x  2 =   8   4 x  2 =   8
   5 x  2 =  10   5 x  2 =  10
-  ** //...lines snipped for space...// **+  ** ...lines snipped for space... **
  11 x 12 = 132  11 x 12 = 132
  -------------  -------------
spo600/64-bit_assembly_language_lab.1707232300.txt.gz · Last modified: 2024/04/16 18:10 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki