CMPT 295: Unit - Machine-Level Programming

Lecture 9 – Assembly language basics: Data, move operation

Last Lecture

  1. Question: How does our C program end up being represented as a series of 0’s and 1’s (i.e., as machine code)?
    • Compiler: C program -> assembly code -> machine level code
    • gcc: 1) C preprocessor, 2) C compiler, 3) assembler, 4) linker
  2. Question: How does our C program (once it is represented as a series of 0’s and 1’s) end up being stored in memory? Annotation: Loader
    • When C program is executed (e.g. from our demo: ./ss 5 6 )
  3. Question: How does our C program (once it is represented as a series of 0’s and 1’s and it is stored in memory) end up being executed by the microprocessor (CPU)?
    • CPU executes C program by looping through the fetch-execute cycle

Summary - Turning C into machine level code - gcc

The big picture:

Annotation: Lab 1

Today’s Menu

Programming in C versus in x86-64 assembly language

When programming in C, we can …

When programming in assembly language, we can do the same things, however …

Programming in x86-64 assembly

Testing a diagram idea

Done with MathML:

CPU=[Registers,Condition Codes,PC]CPUDataMemoryCPUAddressesMemoryCPUinstructionsMemory \text{CPU} = [\text{Registers}, \text{Condition Codes}, \text{PC}] \\ \text{CPU} \xleftrightarrow{Data}{} \text{Memory} \\ \text{CPU} \xrightarrow{Addresses}{} \text{Memory} \\ \text{CPU} \xleftarrow{instructions}{} \text{Memory}

Description: CPU contains registers, condition codes and PC. CPU has a right arrow pointing to memory labled “addresses”. Memory has a left arrow pointing to CPU labled “instructions”. CPU and Memory have a two sides arrow pointing to eachother with the label “data”.

Hum … Why are we learning assembly language?

(empty slide outside of title)

x86-64 Assembly Language - Data

x86-64 Assembly Language – Data

Integer Registers

Integer Registers table (note, the blank first column of the “8-bit” column is intentional):

64-bit (quad) 32-bit (double) 16-bit (word) 8-bit (byte) 8-bit (byte) Transcriber’s Note
63..0 31..0 15..0   7..0  
rax eax ax   al  
rbx ebx bx   bl  
rcx ecx cx   cl  
rdx edx dx   dl  
rsi esi si   sil  
rdi edi di   dil  
rbp ebp bp   bpl  
rsp esp sp   spl  
          All cells above are pink and all cells below are yellow. I don’t know why.
r8 r8d r8w   r8b  
r9 r9d r9w   r9b  
r10 r10d r10w   r10b  
r11 r11d r11w   r11b  
r12 r12d r12w   r12b  
r13 r13d r13w   r13b  
r14 r14d r14w   r14b  
r15 r15d r15w   r15b  

About these integer registers!

Transcriber’s note: 6 slides are compressed into this transcription.

If I want 8 bits worth of data, then I can use register names such as %al or %dil or %r12b

If I want 16 bits worth of data, then I can use register names such as %ax or %di or %r12w

If I want 32 bits worth of data, then I can use register names such as %eax or %edi or %r12d

If I want 64 bits worth of data, then I can use register names such as %rax or %rdi or %r12

Remember that for all 16 registers …

Let’s use the register associated with the names %rax, %eax, %ax and %al as an example:

x86-64 Assembly Language - Instructions

Move data – mov*

Size designators:

Annotation: Homework: movl $0xFF4150AC, %eax (both $0x... and %eax are 32 bits, notice the size designation: l)

  1. Memory reference => Data transfer instructions

Demo – Swap Function

Demo – Swap Function

C code:

void swap(long *xp, long *yp)
{
  long L1 = *xp;
  long L2 = *yp;
  *xp = L2;
  *yp = L1;
  return;
}

Annotation: Because xp and yp contain an address of 64-bits, %rdi & %rsi are used to hold the value of xp and yp, respectively.

Assembly code:

wrap: # xp -> %rdi, yp -> %rsi
  movq    (%rdi), %rax # L1 = *xp; parenthasies indicate "indirect"
  movq    (%rsi), %rdx # L2 = *yp
  movq    %rdx, (%rdi) # *xp = L2
  movq    %rax, (%rsa) # *yp = L1
  ret
Register Address
%rdi 0x0020
%rsi 0x0000
%rax 123
%rdx 456

Remember this is a compressed view of memory.

Address Value
0x0020 (deleted)123 (annotation, inserted)456
0x0018  
0x0010  
0x0008  
0x0000 (deleted)456 (annotation, inserted)123

Operand Combinations for mov*

Source Dest Assembly (instruction (space) source, dest) in C
Immediate Register movq $0x4,%rax result=0x4;
Immediate Memory movq $-147,(%rax) *result=-147;
Register Register movq %rax,%rdx var=result;
Register Memory movq %rax,(%rdx) *var1=result;
Memory Register movq (%rax),%rdx var1=*result;

Cannot do memory-memory transfer with a single mov* instruction

Homework 2

Registers Value
%rdi  
%rsi  
%rax  
%rdx  
Address Value
0x0020  
0x0018  
0x0010  
0x0008  
0x0000 6

Summary

Next Lecture