Last Updated: 2026-03-03 Tue 19:57

CMSC216 Lab05: Basic Assembly Coding

CODE DISTRIBUTION: lab05-code.zip

CHANGELOG:

Tue Mar 3 07:55:19 PM EST 2026

The starter version of coins_funcs_asm.s had a incorrect comment on the layout of the coins_t struct when it is packed into a register. It should read as follows:

  .global total_coins
  total_coins:
  ### args are
  ### %rdi packed coin_t struct with struct fields in the following bit ranges
  ###  {0-15: quarters, 15-31: dimes, 32-47: nickels,  48-63: pennies}
  ### ^^^^^^ THIS LINE UPDATED ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The code in the codepack has been adjusted but those with the old version should just copy in the updated line for guidance. The functional code followed this convention, that all fields are 16 bits wide.

Mon Mar 2 08:59:57 AM EST 2026
The list of Codepack files incorrectly listed some files from last lab and was missing those from the Coins problem. This has been corrected.

1 Rationale

Certain applications utilize bits at an individual level. This is enabled by support for bit-level operations in most processors which are presented in C programs as bit-wise logical and shift operations. This lab explores the basic mechanics of these operations in preparation for project work which will provide a concrete application for them. It also includes a callback to GDB usage to remind students of the insight the debugger can have.

This lab also introduces the basics of assembly having students complete a template of assembly code that mirrors a given C application. This will introduce folks to some syntax and semantics associated with assembly programs.

Associated Reading

  • Bryant and O'Hallaron: Ch 3.1-3.7 on assembly code instructions in x86-64. The first few sections are along with an understanding of argument registers is sufficient to complete the problems.
  • Any overview guide to x86-64 assembly instructions such as Brown University's x64 Cheat Sheet
  • To more easily debug assembly, it is useful to be able to run a debugger like gdb on the assembly code. Examine the Quick Guide to GDB which contains information specific to assembly as such as how to display the register contents.

It is also advisable to configure your editing environment for assembly code. For those using VSCodium, the following short video shows how to install an assembly editing mode that supports the GNU Assembler dialect and shows how to block comment/uncomment code.

Configuring VSCodium / VSCode for x86-64 Assembly: https://youtu.be/AgmXUFOEgIw

Grading Policy

Credit for this lab is earned by completing the code/answers in the Lab codepack and submitting a Zip of the work to Gradescope preferably via running make submit. Students are responsible to check that the results produced locally are reflected on Gradescope after submitting their completed Zip.

Lab Exercises are Free Collaboration and students are encouraged to cooperate on labs. Students may submit work as groups of up to 5 to Gradescope: one person submits then adds the names of their group members to the submission.

No late submissions are accepted for Lab work but the lowest two lab scores for the semester will be dropped including zeros due to missing submissions. See the full policies in the course syllabus.

2 Codepack

The codepack for this lab is linked at the top of this document. Always download it and unzip/unpack it. It should contain the following files which are briefly described.

File Use Description
asmbasics_funcs_asm.s EDIT Problem 1 x86-64 Assembly versions of functions, some must be completed
asmbasics_main.c Provided Problem 1 main function which calls functions which may be in either C or Assembly
asmbasics_funcs.c Provided Problem 1 C versions of functions
coins_funcs_asm.s EDIT Problem 2 Incomplete Assembly functions, fill in remaining code
coins_main.c Provided Problem 2 main() function
coins.h Provided Problem 2 C header
coins_funcs.c Provided Problem 2 C functions to be implemented in assembly, code here can be used to test incrementally
QUESTIONS.txt EDIT Questions to answer: fill in the multiple choice selections in this file.
QUESTIONS.txt.bk Backup Backup copy of the original file to help revert if needed
QUESTIONS.md5 Testing Checksum for answers in questions file
Makefile Build Enables make test and make zip
test_quiz_filter Testing Filter to extract answers from Questions file, used in testing
test_lab05.org Testing Tests for this exercise
testy Testing Test running scripts
gradescope-submit Misc Allows submission to Gradescope from the command line

3 Register Reference

The assembly code studied in this lab involves registers with some special uses. The figure below is helpful to remind of the 16 general purpose registers available in x86-64 architectures, the various names used for them in 16-bit / 32-bit / 64-bit operations, and some of their special uses. Of special note are:

  • Function Arguments / Parameters in Registers: up to 6 arguments are passed in registers and they are labeled in the diagram.
  • Caller vs Callee Save registers: favor use only Caller-Save (blue) registers as this does not require any register save/restore actions which will be studied later.

registers.png

4 QUESTIONS.txt File Contents

Below are the contents of the QUESTIONS.txt file for the exercise. Follow the instructions in it to complete the QUIZ and CODE questions for the exrecise.

                           _________________

                            LAB05 QUESTIONS
                           _________________


Exercise Instructions
=====================

  Follow the instructions below to experiment with topics related to
  this exercise.
  - For sections marked QUIZ, fill in an (X) for the appropriate
    response in this file. Use the command `make test-quiz' to see if
    all of your answers are correct.
  - For sections marked CODE, complete the code indicated. Use the
    command `make test-code' to check if your code is complete.
  - DO NOT CHANGE any parts of this file except the QUIZ sections as it
    may interfere with the tests otherwise.
  - If your `QUESTIONS.txt' file seems corrupted, restore it by copying
    over the `QUESTIONS.txt.bk' backup file.
  - When you complete the exercises, check your answers with `make test'
    and if all is well. Create a zip file and submit it to Gradescope
    with `make submit'. Ensure that the Autograder there reflects your
    local results.
  - IF YOU WORK IN A GROUP only one member needs to submit and then add
    the names of their group on Gradescope.


PROBLEM 1 Background on asmbasics
=================================

  The `asmbasics_*' files allow for the construction of two executables
  which can be built via `make'.

  ,----
  | >> make
  | ...
  | >> ./asmbasics_c_only             # C only version, asmbasics_main.c and asmbasics_funcs.c
  | Let's explore Euclid's formula!
  | ...
  | 
  | >> ./asmbasics_mixed              # Mixed C/Assmebly version: asmbasics_main.c and asmbasics_funcs_asm.s
  | Let's explore Euclid's formula!
  | ...
  `----

  Both executables will do the same thing.
  - Accept to integer on the command line
  - Demonstrate a simple numerical relationship that Euclid worked out
    in the BC era.

  The intent of studying them is to get acquainted with how x86-64
  assembly instructions work by observing C code and equivalent assembly
  code then writing some assembly code that is equivalent to existing C
  code.

  Demoers will tour the slightly odd looking C code in
  `asmbasics_funcs.c' which are very short, arithmetically oriented
  functions. The code is written in an unusual style but one which
  matches the basic arithmetic operations that x86-64 Assembly
  instructions carry out making it easier to see the connection between
  C code and the provided assembly code. These appear in
  `asmbasics_funcs_asm.s' which is an x86-64 assembly source file.

  To pass automated tests, coders will need get a basic understanding of
  how assembly looks and works and then complete the TODO function in
  `asmbasics_funcs_asm.s'.


PROBLEM 1 QUIZ on asmbasics
===========================

  The function code in `asmbasics_funcs.c' looks a little odd because
  - ( ) It uses only operators like +=, *=, -= and so forth
  - ( ) It sets up an `ans' variable in each function to return a value
    rather than just returning something like a+b
  - ( ) Only two variables are used in any arithmetic expression;
    statements like `a = b+c;' do not appear
  - ( ) Actually all of these things are done and while readable, it
    seems inelegant

  On inspecting three provided assembly functions `difference, squared,
  squared_sum' in `asmbasics_funcs_asm.s', these show how many arguments
  and give them names by
  - ( ) Similar to C, they have a prototype like `difference(edi,esi)'
    which shows the registers storing the arguments but not their types
  - ( ) The parameter names are listed below the functions which looks a
    bit strange
  - ( ) They don't list how many and where parameters come in; it's the
    same for all functions and listed in documentation at the top of the
    file without anything in the function code giving clues about it
  - ( ) Trick question: it's actually not possible to pass parameters
    directly in assembly functions

  The equivalent of the `ans' variable used as the return variable in
  the C functions is this thing in x86-64 assembly:
  - ( ) The %eax register
  - ( ) The ret instruction
  - ( ) The %edi register
  - ( ) The addl instruction

  Which of the following is a valid sequence of instructions that will
  complete the `squared_diff' assembly function?
  ,----
  | squared_diff:
  |         movl  %eax, %edi              # (A)
  |         imull %edi, %edi
  |         imull %esi, %esi
  |         subl  %esi, %edi
  |         ret   %edi
  | 
  | squared_diff:
  |         movl  %edi, %eax              # (B)
  |         imull %eax, %eax
  |         imull %esi, %esi
  |         subl  %esi, %eax
  |         ret
  | 
  | squared_diff:
  |         movl  %eax, %edi              # (C)
  |         imull %eax, %eax 
  |         imull %esi, %esi 
  |         subl  %eax, %esi 
  |         ret
  | 
  | squared_diff:
  |         movl  %edi, %eax              # (B)
  |         imull %eax, %eax
  |         imull %esi, %esi
  |         subl  %esi, %eax
  |         ret   %eax
  `----
  - ( ) A
  - ( ) B
  - ( ) C
  - ( ) D


PROBLEM 1 CODE Complete asmbasics_funcs_asm.s
=============================================

  Complete the TODO portions of the code in `asmbasics_funcs_asm.s' and
  run the provided tests. The output should be identical to the behavior
  given when the C versions are run. Experiment with these two
  executables and compare their behavior if in doubt:
  - `asmbasics_c_only' : compiled with C code for main and functions
  - `asmbasics_mixed' : compiled with C code for main and assembly code
    for functions

  Test that the code behaves correctly via the command
  ,----
  | make test-prob1
  `----


PROBLEM 2 QUIZ: Assembly Functions
==================================

  Answer the following questions on basic techniques for assembly
  function coding.

  How are the first 6 arguments to an assembly functions made available
  within the function?
  - ( ) Arguments are passed in registers; the first two are in the
    `%rax' and `%rbx' registers
  - ( ) Arguments are passed in registers; the first two are in the
    `%rdi' and `%rsi' registers
  - ( ) Arguments are in the stack; the first two are available using
    the stack pointer as `0(%rsp)' and `8(%rsp)'
  - ( ) Arguments are in the stack; the first two are available using
    the stack pointer as `8(%rsp)' and `12(%rsp)'

  Which of the following best describes how the kind of arguments passed
  to the FIRST function `set_coins()' function in assembly?
  - ( ) An integer in `%edi' and a pointer to a struct in `%rsi'; to
    get/set fields of the struct syntax like `2(%rsi)' is used
  - ( ) An integer in `%edi' and a packed struct in `%rsi'; to get/set
    fields of the struct bitwise shifts and AND operations are used
  - ( ) An integer in `%esi' and a pointer to a struct in `%rdi'; to
    get/set fields of the struct syntax like `2(%rsi)' is used
  - ( ) An integer in `%esi' and a packed struct in `%rdi'; to get/set
    fields of the struct bitwise shifts and AND operations are used

  Which of the following best describes how the kind of arguments passed
  to the SECOND function `total_coins()' function in assembly?
  - ( ) A pointer to a struct in `%rsi'; to get/set fields of the struct
    syntax like `2(%rsi)' is used
  - ( ) A packed struct in `%rsi'; to get/set fields of the struct
    bitwise shifts and AND operations are used
  - ( ) A pointer to a struct in `%rdi'; to get/set fields of the struct
    syntax like `2(%rsi)' is used
  - ( ) A packed struct in `%rdi'; to get/set fields of the struct
    bitwise shifts and AND operations are used


PROBLEM 2 CODE: Complete coins_funcs_asm.s
==========================================

  Complete the two assembly functions in `coins_funcs_asm.s'. Note that
  until both functions are complete, the tests will likely fail. Correct
  output will appear something like the following:
  ,----
  | >> make
  | ...
  | 
  | >> ./coins_main 88
  | set_coins():
  | 88 cents is...
  | 3 quarters
  | 1 dimes
  | 0 nickels
  | 3 pennies
  | total_coins():
  | which is 88 cents
  `----

  Make sure to complete both assembly functions and test them via
  ,----
  | make test-prob2
  `----

5 Submission

Follow the instructions at the end of Lab01 if you need a refresher on how to upload your completed lab zip to Gradescope.


Author: Chris Kauffman (profk@umd.edu)
Date: 2026-03-03 Tue 19:57