0% found this document useful (0 votes)
44 views22 pages

x86 Assembly Tutorial

This document provides an overview of x86 assembly for a class project. It outlines the schedule and requirements for Project 1, which is due on September 20th. It then details various aspects of x86 assembly programming, including registers, flags, memory addressing, instructions, stack layout, calling conventions, assembler directives, segments, BIOS services, and useful GNU Debugger commands.

Uploaded by

Promix Inc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views22 pages

x86 Assembly Tutorial

This document provides an overview of x86 assembly for a class project. It outlines the schedule and requirements for Project 1, which is due on September 20th. It then details various aspects of x86 assembly programming, including registers, flags, memory addressing, instructions, stack layout, calling conventions, assembler directives, segments, BIOS services, and useful GNU Debugger commands.

Uploaded by

Promix Inc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

x86 Assembly Tutorial

COS 318: Fall 2020


Project 1 Schedule
Deadline to
Design Review Precept 1 Use
x86 Assembly register teams or Project 1
Sessions (Design details andPiazza
Tutorial sign-up for random Due
(15 min per team) Q&A) and OHs.
team assignment

3-7pm ET 7:30-8:20 pm ET 11:55 pm ET


Mon, 9/7 Fri, 9/11 Mon, 9/14 Mon, 9/14 Sun, 9/20
Tue, 9/8
Tue, 9/15 Tue, 9/15

The link will open on 9/12 or 9/13. 2


Overview
● x86 (technically IA-32) Assembly Overview
○ Registers, Flags, Memory Addressing, Instructions, Stack,
Calling Conventions, Directives, Segments

● BIOS (Basic Input/Output System) +


GDB (GNU Debugger)
● Design Review
3
Registers
Segment Registers: 16 bits
General Purpose Registers: 8,16,32 bits (hold 16-bit segment selectors
to identify memory segment)
31 15 7 0
EAX
AH AL AX = AH | AL CS code segment

BH BL BX = BH | BL EBX DS data segment

CH CL CX = CH | CL ECX SS stack segment

DH DL DX = DH | DL EDX ES data segment

BP EBP FS data segment

SI ESI GS data segment

DI EDI Instruction Pointer (EIP): 32 bits


SP ESP Flags (EFLAGS): 32 bits 4
Flags
The 32-bit EFLAGS register:

Flags types:

- Status
- Control
- System

5
● Important Flags
○ CF: Carry flag

○ ZF: Zero flag

○ SF: Sign flag

○ IF: Interrupt flag (sti, cli)


[details: https://en.wikipedia.org/wiki/Interrupt_flag]

○ DF: Direction flag (std, cld)

6
Instruction Syntax Conventions

7
source: http://flint.cs.yale.edu/cs422/doc/pc-arch.html
Memory Addressing
● Memory addressing modes:
1. Real address (unprotected) 2. Protected 3. System Management

● Real address mode:

○ 1MB of memory (20-bit addresses)

○ Valid address range: 0x00000 ~ 0xFFFFF

○ 16-bit segment add. [times 16, i.e., +4 bits] + 16-bit offset add.

8
Memory Addressing (Real Mode)
● Format (AT&T syntax):
○ segment:displacement(base,index,scale)

● Offset = Base + Index * Scale + Displacement

● Address = (Segment * 16) + Offset

● Displacement: Constant

● Base: %bx, %bp

● Index: %si, %di

● Segment: %cs, %ds, %ss, %es, %fs, %gs

9
Data Types

Name Size (bits)


byte 8
word 16
double-word
32
(long in gnu assembler)
quad-word 64

10
Instructions: Arithmetic & Logic
● Arithmetic, such as:
○ add/sub{l,w,b} source,dest
○ inc/dec/neg{l,w,b} dest
○ cmp{l,w,b} source,dest
● Logic, such as:
○ and/or/xor{l,w,b} source,dest ...
● Restrictions
○ No more than one memory operand 11
Instructions: Data Transfer
● mov{l,w,b} source, dest
● xchg{l,w,b} source, dest (exchange)
● movsb/movsw (move byte/word)
○ %es:(%di) ← %ds:(%si)
○ Often used with %cx to move a number of bytes
■ movw $0x10,%cx
■ rep movsw (repeat)

12
Stack Layout
...
● Grows from high to low (function arg. n …)
function arg. 1
○ Lowest address = “top” of stack %bp + 8
return address
%bp + 4
old %ebp
● %sp points to top of the stack local var. 1
%bp
%bp - 4
(local var. n …)
○ Used to reference temporary variables
(callee-save regs)
● %bp points to bottom of stack frame
callee-save reg 1

○ Used for local vars + function args. (temp var. n …)


%sp + 4
temp var. 1
%sp 13
Calling Convention
...

● When calling a function: (function arg. n …)


function arg. 1
○ 1. Push caller-save regs onto stack return address
%bp + 8
%bp + 4
○ 2. Push function args on to stack old %ebp
%bp
local var. 1
○ 3. Push return address + branch %bp - 4
(local var. n …)

● In subroutine: (callee-save regs)


○ 1. Push old %bp + set %bp = %sp callee-save reg 1

○ 2. Allocate space for local variables (temp var. n …)


%sp + 4
○ 3. Push callee-save regs if necessary temp var. 1
%sp 14
Instructions: Stack Access
...

● pushl source (function arg. n …)


function arg. 1
%bp + 8
○ %sp ← %sp - 4 return address
%bp + 4
old %ebp
○ %ss:(%sp) ← source local var. 1
%bp
%bp - 4
● popl dest (local var. n …)

(callee-save regs)
○ dest ← %ss:(%sp)
callee-save reg 1
○ %sp ← %sp + 4 (temp var. n …)
%sp + 4
temp var. 1
%sp 15
Instructions: Control Flow
● jmp label ● call label
○ %eip ← label ○ push %eip
○ %eip ← label
● ljmp NEW_CS, offset
○ %cs ← NEW_CS ● ret
○ %eip ← offset ○ pop %eip

16
Instructions: Conditional Jump
● Relies on %eflags bits
○ Most arithmetic operations change %eflags

● j{e,ne,l,le,g,ge}
○ Jump to label if {=,!=,<,<=,>,>=}

17
Assembler Directives
● Commands that speak directly to the assembler
○ Are not instructions

● Examples:
○ .globl - defines a list of symbols as global
○ .equ - defines a constant (like #define)
○ .bytes, .word, .asciz - reserve space in memory

https://docs.oracle.com/cd/E26502_01/html/E28388/eoiyg.html
18
Assembler Segments
● Organize memory by data properties
○ .text - holds executable instructions
○ .bss - holds zero-initialized data (e.g. static int i;)
○ .data - holds initialized data (e.g. char c = ‘a’;)
○ .rodata - holds read-only data
● Stack / Heap - Set up by linker / loader / programmer
19
Basic Input/Output System (BIOS) Services

● Use BIOS services through int instruction


○ Must store parameters in specified registers
○ Triggers a software interrupt

● int INT_NUM sending a character to the


display at the current
cursor position
○ int $0x10: Video services
○ int $0x13: Disk services
20
Useful GDB Commands
● r - show register values ● b - set a breakpoint

● sreg - show segment registers ● d <n> - delete a breakpoint

● s - step into instruction ● bpd / bpe <n> - disable /


enable a breakpoint
● n - next instruction
● x/Nx addr - display hex dump
● c - continue
of N words, starting at addr
● u <start> <stop> - disassembles
● x/Ni addr - display N
C code into assembly
instructions, starting at addr 21
Design Review
● Write print_char and print_string assembly functions
● Be ready to describe:
○ How to move the kernel from disk to memory
○ How to create disk image
○ (More specific guidelines are provided on the project page)

22

You might also like