Last Updated: 2023-02-21 Tue 09:49

CSCI 2021 HW06: Assembly Basics

CODE DISTRIBUTION: hw06-code.zip

  • Download the code distribution
  • See further setup instructions below

CHANGELOG: Empty

1 Rationale

The basics of working with existing assembly language code are covered in this HW. Generating assembly from C and analyzing it along with adding to existing assembly code and compiling it are covered.

Associated Reading / Preparation

  • Bryant and O'Hallaron: Ch 3.1-3.7 on assembly code instructions in x86-64.
  • Any overview guide to x86-64 assembly instructions such as Brown University's x64 Cheat Sheet
  • Knowledge of how gcc can generate assembly code from C code is required and should be reviewed from the textbook and/or lecture materials.

Grading Policy

Credit for this HW is earned by taking the associated HW Quiz which is linked under Gradescope. The quiz will ask similar questions as those that are present in the QUESTIONS.txt file and those that complete all answers in QUESTIONS.txt should have no trouble with the quiz.

Homework and Quizzes are open resource/open collaboration. You must submit your own work but you may freely discuss HW topics with other members of the class.

See the full policies in the course syllabus.

2 Codepack

The codepack for the HW contains the following files:

File Description
QUESTIONS.txt Questions to answer
ipow_for.c Problem 1 C file for ipow() function
ipow_while.c Problem 1 C file for ipow() function
ipow_main.c Problem 1 main() function
coins_main.c Problem 2 main() function
coins.h Problem 2 C header
coins_funcs.c Problem 2 C functions to be implemented in assembly
coins_funcs_asm.s Problem 2 Incomplete Assembly functions

3 What to Understand

Ensure that you understand

  • How to compile C code to assembly
  • Some basics of registers, operations, and control flow in assembly and how they correspond to C programs
  • Basics of writing assembly code and register passing conventions for functions.

4 Questions

Analyze the files in the provided codepack and answer the questions given in QUESTIONS.txt.

                           _________________

                            HW 06 QUESTIONS
                           _________________


Write your answers to the questions below directly in this text file to
prepare for the associated quiz. Credit for the HW is earned by
completing the associated online quiz on Gradescope.


PROBLEM 1: ipow Assembly
========================

  The code pack contains the following files.
  - ipow_for.c : ipow function using a for loop
  - ipow_while.c : ipow function using a while loop
  - ipow_main.c : main function to call the above
  Both the for and while versions define the same function which has a
  meaning which should be obvious.

  Compile and run these as programs using either of the below:
  ,----
  | > gcc ipow_main.c ipow_for.c
  | > ./a.out 3 5
  | 3^5 = 243
  | 
  | OR
  | 
  | > gcc ipow_main.c ipow_while.c
  | > ./a.out 3 5
  | 3^5 = 243
  `----


A
~

  Compile ipow_for.c to assembly code. Look up how to do this with gcc
  if you did not take notes on how to do so from lecture. Make sure to
  disable optimizations by gcc when compiling.

  Paste the command you used and the generated assembly for the
  resulting ipow_for.s as you answer below.


B
~

  Similarly, compile the file ipow_while.c to assembly and compare the
  resulting ipow_while.s to ipow_for.s.
  - If you see differences, describe what these differences are and why
    the generated code varies between the two.
  - If you see similar code, describe why this might be.


C
~

  Examine the generated assembly code in ipow_for.s carefully. Based on
  the "assignments" of initial values to registers and the use of
  registers in operations like multiplication, addition, and comparison,
  attempt to draw a correspondence between the assembly registers and C
  variables. Ensure you identify the C variables
  - argument base: which register contains the 1st function argument?
  - argument exp: which register contains the 2nd function argument?
  - local pow which is also the return value
  - local i

  For your answer, paste in an annotated copy of the ipow_for.s which
  shows labels the correspondence of registers to C variables and
  describes how most of the assembly lines correspond to lines in the C
  code. You may skip lines like
  ,----
  | .cfi_startproc
  | .type	ipow, @function
  | .size	ipow, .-ipow
  | .ident	"GCC: (GNU) 7.3.0"
  | .section	.note.GNU-stack,"",@progbits
  `----
  which are assembler directives beyond the scope of our current
  interest.


D
~

  One oddity that is likely to appear in the generated assembly code is
  the instruction
  ,----
  | rep ret
  `----

  The `ret' instruction is a function return but `rep' is supposed to
  have a different purpose and the combination is strange.  Investigate
  this by
  - Reading page 208 of Bryant/O'Hallaron which describes this in an
    "aside" , OR
  - Doing a little internet search which should yield similar
    information.
  Describe roughly why `rep ret' shows up here.


PROBLEM 2: Coins Assembly code
==============================

  The code pack contains the following files.

  ------------------------------------------------------------------------
   FILE               Descriptions                                        
  ------------------------------------------------------------------------
   coins_main.c       main function to call the above                     
   coins_funcs.c      two functions manipulating the `coins_t' structure  
   coins_funcs_asm.s  incomplete assembly versions of the above functions 
  ------------------------------------------------------------------------

  The purpose of this problem is to complete the assembly code in
  `coins_funcs_asm.s' so that the functions there act identically to the
  C versions.

  Compile the the C version using the below:
  ,----
  | > gcc coins_main.c coins_funcs.c
  | > ./a.out 24
  | 24 cents is...
  | 0 quarters
  | 2 dimes
  | 0 nickels
  | 4 pennies
  | which is 24 cents
  | 
  | > ./a.out 63
  | 63 cents is...
  | 2 quarters
  | 1 dimes
  | 0 nickels
  | 3 pennies
  | which is 63 cents
  `----

  Edit coins_funcs_asm.s to complete it and answer the questions below.
  After completing the assembly code, the following will compile and
  produce the same results.
  ,----
  | > gcc coins_main.c coins_funcs_asm.s
  | > ./a.out 24
  | 24 cents is...
  | 0 quarters
  | 2 dimes
  | 0 nickels
  | 4 pennies
  | which is 24 cents
  `----


A
~

  Consider the first block of the assembly function marked BLOCK A.
  Describe what it is doing and how it relates to the C code in
  `coins_funcs.c'. Mention the purpose of the code around the label
  .OOB.


B
~

  BLOCK B performs a division operation. Based on the comments and
  assembly operations, describe what is going on, why the %eax register
  is used, and why the movb instruction is employed.


C
~

  Complete BLOCK C by following the pattern laid out in the preceding
  blocks to calculate the number of nickels and pennies required and set
  the fields of the struct pointer to appropriate values. Consider
  carefully whether two additional divisions are required or if a single
  divide will suffice.


D
~

  In the total_coins() function, study the pattern laid out in the first
  few blocks which extract the 8-bit fields from the packed coins_t
  struct in register rdi. Note the use of shifts with AND operations to
  mask the low order bits to get a single field "alone" in a register
  before doing further operations. Mimic this pattern to complete BLOCK
  D which adds on nickels and pennies to the total.

Author: Chris Kauffman (kauffman@umn.edu)
Date: 2023-02-21 Tue 09:49