0% found this document useful (0 votes)
7 views

Assembly

Uploaded by

info.bptrades
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)
7 views

Assembly

Uploaded by

info.bptrades
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/ 49

Assembly

Tr.Pwint Phyu Shwe


26-Oct-2022
Compilation Process
linux> gcc -o hello hello.c

source file - assembly code is highly - Machine langauge


combined like machine specific Assembly
include stdio.h - binary object code
- liner handle different
object code to 1
Hardware Organizaiton of a System
BUS I/O devices Memory

• buses that carry bytes of information • DRAM - Dynamic Random


• System to external world
back and forth between the Access Memory
• keyboard, mouse, monitor, disk drive
components. • Depends on data type store
• IO connects either with adapter(slot)
• bus carry words (8 bytes / 4 bytes) different bytes
or controller (chipsets in
motherboard)

CPU

• Computation unit of computer


• Execute instructions by Program
Counter (PC)
• Operation between Main Memory
(Register file), ALU ,
CPU
Register file

PC ALU

System bus Memory bus

I/O Main “hello, world\n”


Bus interface
bridge memory
hello code

I/O bus
Expansion slots for
other devices such
USB Graphics Disk as network adapters
controller adapter controller

Mouse Keyboard Display

How Simple
hello executable
Disk stored on disk

Figure 1.6 Loading the executable from disk into main memory.

Helloworld.c CPU
Register file

works PC ALU

System bus Memory bus

“hello, world\n”
I/O Main
Bus interface
bridge memory hello code

I/O bus
Expansion slots for
other devices such
USB Graphics Disk as network adapters
controller adapter controller

Mouse Keyboard Display


hello executable
“hello, world\n” Disk stored on disk

Figure 1.7 Writing the output string from memory to the display.
Amdahl’s law.

effectiveness of improving the performance of one part of a system

α=0.6
system that initially consumed 60% of the k=3
time (α=0.6) is sped up by a factor of 3 1/[0.4 + 0.6/3] = 1.67×
(k=3 )
Representing and Manipulating Information
This chapter explains how numbers and other forms of data represented on a comptuer. and
their arithmetic releates to integer and real numbers

• known as two value signal


• computer use server different binary representations to encode numeric values
• bit-level representations
• 8bits is a smallest unit of memory
Hexadecimal Notation
For 8 bits

• binary notation ranges from 00000002 to 111111112


• decimal – 010 to 25510
• base 16 – hex – 0016 – FF16
0 1 2 3 4 5 6 7 8 9 A B C D E F

Common task of machine-level programs to manually convert between decimal, binary


and hexadecimal representation into bit patterns
Hexadecimal calculation
0x – Represents hexa

1010111001001001
0x173A4C

0001 1100 1010 1101 1011 0011


Hexadecimal calculation
Decimal to Hex

3210
Hexadecimal calculation
Decimal to Hex

429496710
Bit Size (Data Size)
• 32 bits word size (4 * 109 bytes) – 4 GB of virtual address space
• 64 bits word size (1.84 * 1019 bytes) – 16 exabytes of virtual address space

When compile code – user can chose machine


codes to depends on type of machine
gcc -m32 prog.c

gcc –m64 prog.c


Bytes Represenation based on Machine
• Big Endian – IBM , Oracle
• Little Endian – most intel compatible machine

12345 Dec - 0x 00 00 30 39 (Hex) 0x12345678


Hexa to Binary – Find matching bit

0x0027C8F8
27C8F8

0x4A1F23E0
Boolean Algebra

NOT AND OR X-OR

• AND – both true then true


• OR – either one true then true
• Not – true then false, false then true
• X-OR – if same false, not same true
Bit-level Operations in C
bitwise Boolean operation
Logical Operation

Unlike bitwise logical operation (&&, ||, !) represent 0 (0x00 – False) and (0x01 – True)
Shift Operations

• left.- shift bits patterns to the left


• right – shift bit partterns to the right
• shift represents with (<<)
• For example: x << k means x will be shifted k bits to the left. the remaining of shifted will
be filled with Zero at the right end
• For example: x >> k means shift k bits to the right.
Integer
Notes: Hex digits 0 to 7 have most significant bit of 0 (non-negative value)
8 to F most significant bit of 1 (negative value)

Hexa Binary Binary to Unsign Binary to 2-Complement


(B2U) Encoding (B2T
Integer
How GCC Compiler generate Assembly Code
linux> gcc -o hello hello.c

- assembly code is highly


source file - Machine langauge
machine specific
combined like Assembly
include stdio.h - binary object code
- liner handle different
object code to 1
Size of Machine
32 bits – 4 gigabytes- 232
64 bits – 256 terabytes – (248- 264)

Command
gcc –Og –S byetesex bytesex.c (assembly code only)
gcc –Og -c bytesex.c (both object code and assembly code)
-Og Follow Original
code level
-S Assembly code

-o Object code
bytesex Output file name
bytesex Execute file
Data Format

movb (move bytes)


movw (move word)
movl (move double word)
movq (move quad word)
movs (move single precision)
movl (move double precision)
16 general purpose registers inside CPU
16-bit operations can
access the least significant
2 bytes, 32-bit operations
can access the least
significant 4 bytes, and
64-bit operations can
access entire registers.
Operand Specifiers

Most instructions have one or more operands specifying the source values to use in performing an
operation and the destination location into which to place the result
Text book Exercises
Exercises

9(%rax, %rdx)

260(%rcx, %rdx)
Data movement instruction
x86 Registers
Stack <data structure> – keep local
variables on memory
First in Last Out

void funcf (){


int x = 10;
int y = 10;
20 }
10
Data movement instructions
label definitoin
mov eax, [ebx] Move the 4 bytes in memory at the address contained in EBX
into EAX
mov [var], ebx Move the contents of EBX into the 4 bytes at memory
address var.
mov eax, [esi-4] Move 4 bytes at memory address ESI + (-4) into EAX
mov [esi+eax], cl Move the contents of CL into the byte at address ESI+EAX
Data movement instructions
label definitoin
push eax push eax on the stack
push [var] push the 4 bytes at address var onto the stack
pop edi pop the top element of the stack into EDI.
pop [ebx] pop the top element of the stack into memory at the four bytes
starting at location EBX.
Data movement instructions
lea — Load effective address

label definitoin
lea edi, [ebx+4*esi] the quantity EBX+4*ESI is placed in EDI
lea eax, [var] the value in var is placed in EAX.
lea eax, [val] the value val is placed in EAX
Data movement instructions
label definitoin
inc <reg> 1. Increment
inc <mem> 2. decrement
dec <reg>
dec <mem>
INC COUNT 1. Increment the memory variable COUNT
dec eax 2. subtract one from the contents of EAX.

add <reg>,<reg> 1. Add the content of the


add <reg>,<mem>
add <mem>,<reg>
add <reg>,<con>
add <mem>,<con>
add eax, 10 1. Perform ADD operation on the
2. EAX ← EAX + 10
add BYTE PTR [var], 10 1. add 10 to the single byte stored at memory address var
Data movement instructions
label definitoin
imul Integer Multiplication
imul eax, [var] 1. multiply the contents of EAX by the 32-bit contents of
imul esi, edi, 25 the memory location var. Store the result in EAX.
idiv Integer Division
and, xor, not 1. Bitwise logical and, or and exclusive or
2. Bitwise Logical Not
shl, shr Shift Left, Shift Right (2n)
Control flow
label definitoin
jmp Jump flow
jcondition Condition Jump
je<label> 1. jump when equal
jne<label> 2. jump when not equal
jz<label> 3. jump when last result was zero
jg, jge,jl,jle 4. jump greater than/greater equal to/less than less than
equal to
cmp Compare
cmp <reg>,<reg> 1. Bitwise Logical Not
cmp <reg>,<mem>
cmp <mem>,<reg>
cmp <reg>,<con>
call, ret Subroutine call and return
# y in %rdi, z in %rsi, x in $rax
int f (){
int x = y + z; addl %rdi, %rsi
return x; movl %rax, %rdi
} ret

int main() { main:


int n=5, movl $5, %eax
long fact = 1; movl $1, %ebx
L1: cmpl $0, %eax
int i = 0;
je L2
imull %eax, %ebx
for (int i = n; i > 0; i--){ decl %eax
fact *= i; jmp L1
} L2: ret
return 0;
}
int foo(){ main:
return x+5; movl $10, %eax
} call foo
ret
int main() {
foo:
foo(); addl $5, %eax
return 0; ret
}
Exercise
Exercise – data movement
Exercises
Write c code based on assembly
Linker

• The linker combines different parts of programs into a single file


• loaded into memory and executed by the processor
• linker performed on compile time while source code is translated to machine code
• Linking is usually handled quietly by the linker
• More efficient and effective for large skill systems where code written in modules or in
different files and liner combined
Example
sumnumber.c sum.c
#include<stdio.h> int sum(int x, int y){
return x+y;
int sum(int,int); }
int main(){
int z = sum(5,4);
printf ("Result %d - ", z); gcc -og -o sumex sumnumber.c sum.c
return 0;
}
Static Linking
• take as input a collection of relocatable object files
• input relocatable object files consist of various code and data sections
• each section is a contiguous sequence of bytes
• Instructions are in one section
• initialized global variables are in another section
• uninitialized variables are in yet another section
• Object files are merely collections of blocks of bytes.
Relocable section defintion
object files .text The machine code of the compiled program

.rodata Read-only data such as the format strings in printf


statements, and
jump tables for switch statements.
.data Initialized global and static C variables <1 time
across system>
.bss Uninitialized static variables, and global or static
variables that are initialized to zero
COMMON Uninitialized global variables

.symtab Symbol table for functions and global data


reference
rel.text Location in .text section that will need to be
modified
.rel.data Relocation information for any global variables
which defines in any modules
.debug .debug table

.line Mapping of line between orginal c code and


assembly code <only present in –g option used>
.strtab String table for null terminated character string
Linker symbols
Two symbols

-> Golbal symbols – global variables and non static function data
-> local symbols – static attributes global variables and static function data
<in c programming> if global variables are declared with static in the module that
mean it can only be available in module level not into global level (One way to
hide value by programmer>

• Linker doesn’t store information of local variables data. Local variable data are store in Stack.
Exercises

You might also like