CMPS-2240 Lab 10
Intro to x86-64 Assembly

Gordon Griesel
Department of Computer and Electrical Engineering and Computer Science
California State University, Bakersfield

Introduction

Goals:
Learn to write an x86-64 assembly language program.

Files Needed:
All files at: https://cs.csub.edu/~gordon/2240/code/lab10/
Copy them to your Odin 2240/a folder like this:


cd
cd 2240/a
cp /home/fac/gordon/public_html/2240/code/lab10/* .

Description

This lab introduces you to x86-64 assembly using NASM or Yasm. Both are x86 assemblers. Yasm has a few more capabilities than NASM. For the purposes of this class, either assembler will work fine.

Your job is to modify lab10.asm. The source currently prompts the user to input two integers from the keyboard and displays the greater of the two. Please modify the assembly code to:
. Display the sum of the two numbers.
. Display a message indicating when the numbers are equal.

Since integer I/O requires a conversion routine to convert from char to int you are going to link in the gcc standard c library and use printf and scanf rather than use assembly code to do this. Since the executable can be run on Odin, you can use gdb to debug your code. Note that you are linking a program written in C and a program written in x86 assembly.
This is ok because gcc will compile the C program into x86 machine language, and NASM/Yasm will compile the assembly source mnemonics into the same kind of machine language. Thus, the two programs can interact with each other as long as they follow the same conventions.
As we learned with MIPS, function arguments and output return values must be placed in specific registers for programs to work together. This is the calling convention.

Before beginning, study the comments on each line of code in bare.asm and inout.asm.

inout.asm prompts the user to enter a string from the keyboard (stdin) and then displays the entire string that the user entered to stdout (the screen). An x86-64 assembly file under linux will be assembled by nasm or yasm into an elf64 object file and then linked by ld into an elf64 executable. The file can then be executed on Odin. Some console commands to do this for nasm are:


 nasm -f elf64 lab10.asm   # this produces lab10.o
 gcc -o lab10 lab10.o      # gcc calls the linker

There is a convenient way to assemble and link your code. Since we are writing in x86 on an x86 machine we can use the make utility to handle the assembling and linking commands.

The copy commands above already placed a Makefile in your own folder.

So, to assemble and link inout.asm using the Makefile do this:

make inout

To remove .o and executables do this:

make clean

The code you need to complete this lab is in lab10.asm. x86-64 is CISC, is not load/store and is backwards compatible with 8 or so ISAs. "Not load/store" means that most instructions allow one of the operands to be a memory address. These three reasons make the x86 ISA instruction set larger than the MIPS instruction set.

Some programmers find that x86 is more difficult to understand than MIPS, but it really all depends on the individual.

One thing to notice is that the 64-bit register names in x86-64 begin with 'R' instread of 'E'. Add the instructions below into your code. Use printf to display the result. You can keep all existing code in lab10.asm.


When you first run make, the output might have warnings like below.
Try to fix the warnings. Notes are in the Makefile.
gcc -o lab10 lab10.o -no-pie -lc
--------------------------------
/usr/bin/ld: warning: lab10.o: missing .note.GNU-stack section implies executable stack
/usr/bin/ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linker

gcc -c driver.c -o driver.o
---------------------------
driver.c: In function 'main':
driver.c:17:3: warning: implicit declaration of function 'print_string' [-Wimplicit-function-declaration]
   17 |   print_string(mystr);
      |   ^~~~~~~~~~~~

gcc -o driver io.o driver.o -no-pie
-----------------------------------
/usr/bin/ld: warning: io.o: missing .note.GNU-stack section implies executable stack
/usr/bin/ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linker

Some sample runs are below.


$ make
nasm -f elf64 -g -F dwarf lab10.asm    # -g -F dwarf adds debugging stuff
gcc -o lab10  lab10.o -lc


$ ./lab10

Enter an integer: 21
Enter another integer: 5
The greater of 21 and 5 is 21.
The sum of 21 and 5 is 26.


$ ./lab10

Enter an integer: 23
Enter another integer: 23
23 and 23 are equal.
The sum of 23 and 23 is 46.


$ ./lab10

Enter an integer: 23
Enter another integer: 23
Your two numbers are equal.
The sum of 23 and 23 is 46.

What to turn in

Gordon will copy these programs at end of lab...
   2240/a/lab10.asm
   2240/a/Makefile
   plus...
   2240/a/bare.asm
   2240/a/driver.c
   2240/a/goodbye.asm
   2240/a/inout.asm
   2240/a/io.asm