Machine-Level Programming I - Basics

Class: CSCE-312


Notes:

Today: Machine Programming I: Basics

History of Intel processors and architectures

Intel x86 Processors

Intel x86 Evolution: Milestones

Name Date Transistors MHz

Intel x86 Processors, cont.

Pasted image 20250916125234.png|275

2015 State of the Art

x86 Clones: Advanced Micro Devices (AMD)

Intel's 64-Bit History

...

C, assembly, machine code

Definitions

Assembly/Machine Code View

Machine_Code_View.png|400

Programmer-Visible State

Turning C into Object Code

From_C_to_Object_Code.png|500

Compiling into Assembly

C Code (sum.c)

long plus(long x, long y); 

void sumstore(long x, long y, 
              long *dest)
{
    long t = plus(x, y);
    *dest = t;
}

Obtain with command

gcc –O –S sum.c

Generated x86-64 Assembly

sumstore:
   pushq   %rbx
   movq    %rdx, %rbx
   call    plus
   movq    %rax, (%rbx)
   popq    %rbx
   ret

**Warning****: Will get very different results on other machines due to different versions of gcc and different compiler settings.

Assembly Characteristics: Data Types

Assembly Characteristics: Operations

Object Code

Code for sumstore

0x0400595: 
   0x53
   0x48
   0x89
   0xd3
   0xe8
   0xf2
   0xff
   0xff
   0xff
   0x48
   0x89
   0x03
   0x5b
   0xc3

Machine Instruction Example

C Code

*dest = t:

Assembly

movq %rax, (%rbx)

Object Code

0x40059e:  48 89 03

Disassembling Object Code

Disassembled

0000000000400595 <sumstore>:
  400595:  53               push   %rbx
  400596:  48 89 d3         mov    %rdx,%rbx
  400599:  e8 f2 ff ff ff   callq  400590 <plus>
  40059e:  48 89 03         mov    %rax,(%rbx)
  4005a1:  5b               pop    %rbx
  4005a2:  c3               retq

Disassembler
objdump –d sum

Alternate Disassembly

Object

0x0400595: 
   0x53
   0x48
   0x89
   0xd3
   0xe8
   0xf2
   0xff
   0xff
   0xff
   0x48
   0x89
   0x03
   0x5b
   0xc3

Dissasembly

Dump of assembler code for function sumstore:
 0x0000000000400595 <+0>: push   %rbx
 0x0000000000400596 <+1>: mov    %rdx,%rbx
 0x0000000000400599 <+4>: callq  0x400590 <plus>
 0x000000000040059e <+9>: mov    %rax,(%rbx)
 0x00000000004005a1 <+12>:pop    %rbx
 0x00000000004005a2 <+13>:retq 

What Can be Disassembled?

% objdump -d WINWORD.EXE

WINWORD.EXE:   file format pei-i386

No symbols in "WINWORD.EXE".
Disassembly of section .text:

30001000 <.text>:
30001000:  55             push   %ebp
30001001:  8b ec          mov    %esp,%ebp
30001003:  6a ff          push   $0xffffffff
30001005:  68 90 10 00 30 push   $0x30001090
3000100a:  68 91 dc 4c 30 push   $0x304cdc91

Reverse engineering forbidden by Microsoft End User License Agreement

Assembly Basics: Registers, operands, move

x86-64 Integer Registers

Pasted image 20250918124904.png|500

Some History: IA32 Registers

Pasted image 20250918130142.png|600

Moving Data

Pasted image 20250918133345.png|150

movq Operand Combinations

Pasted image 20250918133948.png|600

Cannot do memory-memory transfer with a single instruction

Source

Destination

Simple Memory Addressing Modes

Example of Simple Addressing Modes

C Code

void swap
   (long *xp, long *yp) 
{
  long t0 = *xp;
  long t1 = *yp;
  *xp = t1;
  *yp = t0;
}

Assembly

swap:
   movq    (%rdi), %rax
   movq    (%rsi), %rdx
   movq    %rdx, (%rdi)
   movq    %rax, (%rsi)
   ret

Understanding swap()

void swap
   (long *xp, long *yp) 
{
  long t0 = *xp;
  long t1 = *yp;
  *xp = t1;
  *yp = t0;
}

Pasted image 20250925130015.png|300

Register	Value
%rdi	    xp
%rsi	    yp
%rax	    t0
%rdx	    t1
swap:
   movq    (%rdi), %rax  # t0 = *xp  
   movq    (%rsi), %rdx  # t1 = *yp
   movq    %rdx, (%rdi)  # *xp = t1
   movq    %rax, (%rsi)  # *yp = t0
   ret

...

Complete Memory Addressing Modes

Address Computation Examples

%rdx 0xf000
%rcx 0x0100
![Pasted image 20250918135311.png 450](/img/user/00%20-%20TAMU%20Brain/5th%20Semester%20(Fall%2025)/CSCE-312/Visual%20Aids/Pasted%20image%2020250918135311.png)
  1. 4 bytes each
  2. 0x80(,%rdx,2)
    • Ommiting base register
      ...

Arithmetic & logical operations

Address Computation Instruction

Example

long m12(long x)
{
  return x*12;
}

Converted to ASM by compiler:

leaq (%rdi,%rdi,2), %rax  # t <- x+x*2
salq $2, %rax             # return t<<2

Some Arithmetic Operations

Two Operand Instructions:

  Format	Computation
	addq	Src,Dest	Dest = Dest + Src
	subq	Src,Dest	Dest = Dest − Src
	imulq	Src,Dest	Dest = Dest * Src
	salq	Src,Dest	Dest = Dest << Src	# Also called shlq
	sarq	Src,Dest	Dest = Dest >> Src	# Arithmetic (signed)
	shrq	Src,Dest	Dest = Dest >> Src	# Logical (unsigned)
	xorq	Src,Dest	Dest = Dest ^ Src
	andq	Src,Dest	Dest = Dest & Src
	orq	Src,Dest	Dest = Dest | Src

One Operand Instructions

incq	Dest	Dest = Dest + 1
decq	Dest	Dest = Dest − 1
negq	Dest	Dest = − Dest    (takes two's complement)
notq	Dest	Dest = ~Dest     (takes one's complement)

Arithmetic Expression Example

C code:

long arith
(long x, long y, long z)
{
  long t1 = x+y;
  long t2 = z+t1;
  long t3 = x+4;
  long t4 = y * 48;
  long t5 = t3 + t4;
  long rval = t2 * t5;
  return rval;
}

Assembly code:

arith:
  leaq    (%rdi,%rsi), %rax      # t1
  addq    %rdx, %rax             # t2
  leaq    (%rsi,%rsi,2), %rdx        # (y * 3)
  salq    $4, %rdx               # t4: (y * 3) * 16
  leaq    4(%rdi,%rdx), %rcx     # t5: t3 + t4
  imulq   %rcx, %rax             # rval
  ret

Interesting Instructions

Understanding Arithmetic Expression Example

Register Use(s)
%rdi Argument x
%rsi Argument y
%rdx Argument z
%rax t1, t2, rval
%rdx t4
%rcx t5

Machine Programming I: Summary