Reading ¡assignment ¡
- Chapter ¡3.1, ¡3.2 ¡
- Chapter ¡4.1, ¡4.3 ¡
1 ¡
Reading assignment Chapter 3.1, 3.2 Chapter 4.1, 4.3 1 - - PowerPoint PPT Presentation
Reading assignment Chapter 3.1, 3.2 Chapter 4.1, 4.3 1 Outline Introduc5on to assembly programing Introduc5on to Y86 Y86 instruc5ons, encoding
1 ¡
2 ¡
3 ¡
4 ¡
5 ¡
– Format ¡and ¡behavior ¡of ¡a ¡machine ¡level ¡program ¡ Defines: ¡
– Abstrac5ons ¡
– Technically ¡defined ¡to ¡be ¡comple5ng ¡one ¡instruc5on ¡before ¡star5ng ¡the ¡next ¡ – Pipelining ¡ – Concurrent ¡execu5on ¡(but ¡not ¡really) ¡
– Very ¡large ¡byte-‑addressable ¡array ¡ – Address ¡space ¡managed ¡by ¡the ¡OS ¡(virtual ¡à ¡physical) ¡ – Contains ¡both ¡executable ¡code ¡of ¡the ¡program ¡AND ¡its ¡data ¡ » Run-‑5me ¡stack ¡ » Block ¡of ¡memory ¡for ¡user ¡(global ¡and ¡heap) ¡
6 ¡
7 ¡
– Register ¡%eip ¡(X86-‑64) ¡ – Address ¡in ¡memory ¡of ¡the ¡next ¡instruc5on ¡to ¡be ¡executed ¡
– Contains ¡eight ¡named ¡loca5ons ¡for ¡storing ¡32-‑bit ¡values ¡
– Hold ¡status ¡informa5on ¡
– CF ¡(carry ¡flag) ¡ – OF ¡(overflow ¡flag) ¡ – SF ¡(sign ¡flag) ¡ – ZF ¡(zero ¡flag) ¡
8 ¡
9 ¡
10 ¡
11 ¡
12 ¡
hsp://voices.yahoo.com/the-‑y86-‑processor-‑simulator-‑770435.html?cat=15 ¡ hsp://y86tutoring.wordpress.com/ ¡
13 ¡
low ¡address ¡ high ¡address ¡ 1. A ¡huge ¡array ¡of ¡bytes; ¡ 2. Set ¡the ¡bosom ¡of ¡the ¡stack ¡ far ¡enough ¡away ¡from ¡the ¡ code; ¡ 3. The ¡loca5on ¡of ¡your ¡code ¡ should ¡always ¡start ¡from ¡
¡ How ¡to ¡set ¡up ¡the ¡star5ng ¡point ¡
direc5ve: ¡.pos ¡address-‑in-‑hex ¡
– /home/f85/bren/Sovware/sim/misc ¡ – /home/f85/bren/Sovware/sim/pipe ¡ – /home/f85/bren/Sovware/sim/seq ¡ – The ¡example ¡code ¡was ¡assembled ¡during ¡the ¡build ¡process ¡and ¡is ¡in ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/home/f85/bren/Sovware/sim/y86-‑code. ¡ ¡
– %yas ¡prog.ys ¡
– %yis ¡prog.yo ¡
– %ssim ¡–g ¡ ¡prog.yo ¡& ¡
– linkà ¡ ¡hsp://csapp.cs.cmu.edu/public/simguide.pdf ¡
14 ¡
irmovl ¡$55,%edx ¡ rrmovl ¡%edx, ¡%ebx ¡ irmovl ¡Array, ¡%eax ¡ rmmovl ¡%ebx,4(%eax) ¡ mrmovl ¡0(%eax),%ecx ¡ halt ¡ .align ¡4 ¡ Array: ¡ .long ¡0x6f ¡ .long ¡0x84 ¡
15 ¡
% ¡yas ¡y86prog1.ys ¡ % ¡yis ¡y86prog1.yo ¡ Stopped ¡in ¡6 ¡steps ¡at ¡PC ¡= ¡0x1a. ¡ Status ¡'HLT' ¡ CC ¡Z=1 ¡S=0 ¡O=0 ¡ Changes ¡to ¡registers: ¡ %eax: ¡0x00000000 ¡0x0000001c ¡ %ecx: ¡0x00000000 ¡0x0000006f ¡ %edx: ¡0x00000000 ¡0x00000037 ¡ %ebx: ¡0x00000000 ¡0x00000037 ¡ ¡ Changes ¡to ¡memory: ¡ 0x0020: ¡0x00000084 ¡0x00000037 ¡
y86prog1.ys ¡
16 ¡
17 ¡
IA32; ¡but ¡not ¡as ¡compact ¡(as ¡we ¡will ¡see) ¡
– 8 ¡32-‑bit ¡registers ¡with ¡the ¡same ¡names ¡as ¡the ¡IA32 ¡32-‑bit ¡registers ¡ ¡ – 3 ¡condi5on ¡codes: ¡ZF, ¡SF, ¡OF ¡ ¡
– a ¡program ¡counter ¡(PC) ¡
– a ¡program ¡status ¡byte: ¡AOK, ¡HLT, ¡ADR, ¡INS ¡ ¡
– memory: ¡up ¡to ¡4 ¡GB ¡to ¡hold ¡program ¡and ¡data ¡
18 ¡
%eax %ecx %edx %ebx %esi %edi %esp %ebp RF: Program registers ZF SF OF
CC: Condition codes PC
DMEM: Memory
Stat: Program Status
19 ¡
20 ¡
21 ¡
22 ¡
– How ¡would ¡this ¡happen? ¡ – This ¡generates ¡an ¡excep5on. ¡
– On ¡a ¡real ¡system, ¡this ¡would ¡be ¡handled ¡by ¡the ¡OS ¡and ¡only ¡the ¡current ¡process ¡ would ¡be ¡terminated. ¡
– Invalid ¡opera5on ¡ ¡ – Divide ¡by ¡0 ¡ ¡ – sqrt ¡of ¡nega5ve ¡number ¡ ¡ – Memory ¡access ¡error ¡(address ¡too ¡large) ¡ ¡ – Hardware ¡error ¡ ¡
– HLT ¡instruc5on ¡executed ¡ ¡ – Invalid ¡address ¡encountered ¡ ¡ – Invalid ¡instruc5on ¡encountered ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡In ¡each ¡case ¡the ¡status ¡is ¡set ¡
23 ¡
– Includes ¡only ¡4-‑byte ¡integer ¡opera5ons ¡à ¡“word” ¡ – Has ¡fewer ¡addressing ¡modes ¡ – Smaller ¡set ¡of ¡opera5ons ¡
– 1–6 ¡bytes ¡of ¡informa5on ¡read ¡from ¡memory ¡
– rA ¡or ¡rB ¡represent ¡one ¡of ¡the ¡registers ¡(0-‑7) ¡ – 0xF ¡denotes ¡no ¡register ¡(when ¡needed) ¡ – No ¡par5al ¡register ¡op5ons ¡(must ¡be ¡a ¡byte) ¡
24 ¡
25 ¡
– register ¡to ¡register ¡ ¡(opcode ¡= ¡2) ¡
– immediate ¡to ¡register ¡ ¡(opcode ¡= ¡3) ¡ – register ¡to ¡memory ¡ ¡(opcode ¡= ¡4) ¡ – memory ¡to ¡register ¡ ¡(opcode ¡= ¡5) ¡
26 ¡
CORRECTION ¡= ¡F ¡
27 ¡
# ¡y86cc.ys ¡ .pos ¡0x0 ¡ irmovl ¡ ¡ ¡$1, ¡%eax ¡ irmovl ¡ ¡ ¡$0, ¡%ebx ¡ irmovl ¡ ¡ ¡$1, ¡%ecx ¡ addl ¡%eax, ¡%eax ¡ andl ¡%ebx, ¡%ebx ¡ subl ¡%eax, ¡%ecx ¡ irmovl ¡ ¡$0x7fffffff, ¡%edx ¡ addl ¡%edx, ¡%edx ¡ halt ¡
28 ¡
– jmp ¡Dest ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡PC ¡← ¡Dest ¡
– jle ¡Dest ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡PC ¡← ¡Dest ¡if ¡last ¡result ¡≤ ¡0 ¡
– jl ¡Dest ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡PC ¡← ¡Dest ¡if ¡last ¡result ¡< ¡0 ¡ ¡
– je ¡Dest ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡PC ¡← ¡Dest ¡if ¡last ¡result ¡= ¡0 ¡
– jne ¡Dest ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡PC ¡← ¡Dest ¡if ¡last ¡result ¡≠ ¡0 ¡
– jge ¡Dest ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡PC ¡← ¡Dest ¡if ¡last ¡result ¡≥ ¡0 ¡
– jg ¡Dest ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡PC ¡← ¡Dest ¡if ¡last ¡result ¡> ¡0 ¡
29 ¡
If ¡the ¡last ¡ result ¡is ¡not ¡ what ¡is ¡ specified, ¡ then ¡the ¡jump ¡ is ¡not ¡taken; ¡ and ¡the ¡next ¡ sequen5al ¡ instruc5on ¡is ¡ executed ¡i.e. ¡ PC ¡= ¡PC ¡+ ¡ jump ¡ instruc5on ¡ size ¡vice ¡Dest ¡
What ¡about ¡checking ¡ OF? ¡
30 ¡
Which ¡instruc5ons ¡set ¡the ¡CC ¡bits? ¡ What ¡are ¡the ¡flags ¡set ¡to ¡ for ¡each ¡instruc5on? ¡
31 ¡
32 ¡
The ¡cmovxx ¡statement ¡only ¡moves ¡ the ¡source ¡register ¡value ¡to ¡the ¡ des5na5on ¡register ¡if ¡“the ¡ condi5on ¡is ¡true”, ¡so: ¡ ¡ If ¡the ¡condi5on ¡is ¡“equal”, ¡that ¡ means ¡the ¡CC ¡bits ¡have ¡the ¡ZF ¡set ¡ to ¡1 ¡i.e. ¡the ¡previous ¡result ¡was ¡ equal ¡to ¡zero, ¡ ¡ cmovg ¡– ¡checks ¡if ¡the ¡previous ¡ result ¡was ¡greater ¡than ¡zero ¡(i.e. ¡ SF=0) ¡and ¡if ¡so, ¡moves ¡the ¡source ¡ register ¡value ¡to ¡the ¡des5na5on ¡ register ¡ ¡ ETC ¡
program ¡data ¡
suppor5ng ¡procedure ¡calls ¡
%esp
– Address ¡of ¡top ¡stack ¡ element ¡
addresses ¡
– Top ¡element ¡is ¡at ¡highest ¡ address ¡in ¡the ¡stack ¡ – When ¡pushing, ¡must ¡first ¡ decrement ¡stack ¡pointer ¡ – When ¡popping, ¡increment ¡ stack ¡pointer ¡
33 ¡
%esp
Addresses ¡
Stack ¡“Top” ¡ Stack ¡ “Bosom” ¡ %esp
Addresses ¡
Stack ¡“Bosom” ¡ Stack ¡“Top” ¡
34 ¡
¡ ¡ ¡ ¡ ¡ ¡ rA ¡ <-‑-‑ ¡%esp-‑4 ¡ Stack: ¡ ¡ ¡ <-‑-‑ ¡%esp ¡ pushl ¡rA ¡ ¡ ¡ ¡ ¡ ¡ ¡ value ¡ <-‑-‑ ¡%esp ¡ Stack: ¡ ¡ ¡ <-‑-‑ ¡%esp+4 ¡ popl ¡rA ¡ rA ¡<-‑-‑ ¡value ¡ R[rA]←M[R[%esp]] ¡ R[%esp]←R[%esp]+4 ¡ R[%esp]←R[%esp]-‑4 ¡ M[R[%esp]]←R[rA] ¡
35 ¡
Note: ¡call ¡uses ¡absolute ¡addressing ¡
– Dest ¡R[%esp]←R[%esp]-‑4 ¡
– M[R[%esp]]←PC ¡
incremented ¡to ¡the ¡next ¡instruc5on, ¡and ¡store ¡it ¡ in ¡the ¡memory ¡loca5on ¡pointed ¡to ¡by ¡reg ¡%esp ¡
– PC←Dest ¡
being ¡called ¡into ¡the ¡PC ¡
– PC←M[R[%esp]] ¡
– R[%esp]←R[%esp]+4 ¡
36 ¡
%esp
Addresses ¡
Stack ¡“Top” ¡
37 ¡
38 ¡
39 ¡
u State ¡
Data: ¡read ¡and ¡write ¡ Instruc5on: ¡read ¡
u Instruc5on ¡Flow ¡
specified ¡by ¡PC ¡
u Fetch ¡
instruc5on ¡memory ¡
as ¡instruc5on ¡
u Decode ¡
u Execute ¡
u Memory ¡
u Write ¡Back ¡
u PC ¡
– Read ¡2 ¡bytes ¡
– Read ¡operand ¡ registers ¡
– Perform ¡opera5on ¡ – Set ¡condi5on ¡codes ¡
– Do ¡nothing ¡
– Update ¡register ¡
– Increment ¡PC ¡by ¡2 ¡
40 ¡
– Read ¡6 ¡bytes ¡
– Read ¡operand ¡ registers ¡
– Compute ¡effec5ve ¡ address ¡
– Write ¡to ¡memory ¡
– Do ¡nothing ¡
– Increment ¡PC ¡by ¡6 ¡
41 ¡
42 ¡
Fetch ¡
Read ¡2 ¡bytes ¡
Decode ¡
Read ¡stack ¡ pointer ¡
Execute ¡
Increment ¡stack ¡ pointer ¡by ¡4 ¡
Memory ¡
Read ¡from ¡old ¡ stack ¡pointer ¡
Write ¡back ¡
Update ¡stack ¡ pointer ¡ Write ¡result ¡to ¡ register ¡
PC ¡Update ¡
Increment ¡PC ¡ by ¡2 ¡ F ¡
43 ¡
Fetch ¡
Read ¡5 ¡bytes ¡ Increment ¡PC ¡by ¡5 ¡
Decode ¡
Do ¡nothing ¡
Execute ¡
Determine ¡ whether ¡to ¡take ¡ branch ¡based ¡on ¡ jump ¡condi5on ¡ and ¡condi5on ¡ codes ¡
Memory ¡
Do ¡nothing ¡
Write ¡back ¡
Do ¡nothing ¡
PC ¡Update ¡
Set ¡PC ¡to ¡Dest ¡if ¡ branch ¡taken ¡or ¡to ¡ incremented ¡PC ¡if ¡ not ¡branch ¡
44 ¡
Fetch ¡
Read ¡5 ¡bytes ¡ Increment ¡PC ¡by ¡5 ¡
Decode ¡
Read ¡stack ¡pointer ¡
Execute ¡
Decrement ¡stack ¡ pointer ¡by ¡4 ¡
Memory ¡
Write ¡incremented ¡ PC ¡to ¡new ¡value ¡of ¡ stack ¡pointer ¡
Write ¡back ¡
Update ¡stack ¡ pointer ¡
PC ¡Update ¡
Set ¡PC ¡to ¡Dest ¡
45 ¡
Fetch ¡
Read ¡5 ¡bytes ¡ Increment ¡PC ¡by ¡5 ¡
Decode ¡
Read ¡stack ¡pointer ¡
Execute ¡
Decrement ¡stack ¡ pointer ¡by ¡4 ¡
Memory ¡
Write ¡incremented ¡ PC ¡to ¡new ¡value ¡of ¡ stack ¡pointer ¡
Write ¡back ¡
Update ¡stack ¡ pointer ¡
PC ¡Update ¡
Set ¡PC ¡to ¡Dest ¡
46 ¡
47 ¡
¡
¡
48 ¡