Pertemuan 7 Bahasa Rakitan: III

Slides:



Advertisements
Similar presentations
COMP 2003: Assembly Language and Digital Logic
Advertisements

NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.c: Logical Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
8086 : INSTRUCTION SET By, Pramod Sunagar Assistant Professor
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 5 Arithmetic and Logic Instructions.
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Microcomputer & Interfacing Lecture 3
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Ch. 7 Logic, Shift and Rotate instr.
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
Microprocessor Dr. Rabie A. Ramadan Al-Azhar University Lecture 7.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Arithmetic Flags and Instructions
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.b: Arithmetic Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
26-Nov-15 (1) CSC Computer Organization Lecture 6: Pentium IA-32.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Arithmetic Flags and Instructions Chapter 7 S. Dandamudi.
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
MOV Instruction MOV destination,source  MOV AX,BX  MOV SUM,EAX  MOV EDX,ARRAY[EBX][ESI]  MOV CL,5  MOV DL,[BX]
Microprocessor & Assembly Language Arithmetic and logical Instructions.
1 Pertemuan 5 Bahasa Rakitan: I Matakuliah: T0324 / Arsitektur dan Organisasi Komputer Tahun: 2005 Versi: 1.
Micro-Computer Applications: Arithmetic, Logic & Data Movement Instructions Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
Assembly Language Addressing Modes. Introduction CISC processors usually supports more addressing modes than RISC processors. –RISC processors use the.
Computer Architecture and Assembly Language
Chapter Nov-2010
Data Transfers, Addressing, and Arithmetic
16.317: Microprocessor System Design I
Homework Reading Labs PAL, pp
Today we are going to discuss about,
Microprocessor Systems Design I
Chapter 4 Data Movement Instructions
Microprocessor Systems Design I
EE3541 Introduction to Microprocessors
Basic Assembly Language
INSTRUCTION SET.
Machine control instruction
Assembly IA-32.
INSTRUCTION SET.
Assembly Language Programming Part 2
# include < stdio.h > v oid main(void) { long NUM1[5]; long SUM; long N; NUM1[0] = 17; NUM1[1] = 3; NUM1[2] =  51; NUM1[3] = 242; NUM1[4] = 113; SUM =
Microcomputer Programming
Overview Introduction General Register Organization Stack Organization
INSTRUCTION SET OF 8086 PAWAN KUMAR SINGH.
CS 301 Fall 2002 Assembly Instructions
X86’s instruction sets.
Computer Architecture adapted by Jason Fritts then by David Ferry
Y86 Processor State Program Registers
Assembly Language: IA-32 Instructions
Figure 2.1. Binary, signed-integer representations.
CS 301 Fall 2002 Computer Organization
Homework Reading Machine Projects Labs PAL, pp
(Assume top of stack is at level 1 b elo w.) Calling program LDR R0,POINTER Push NUM1 STR R0,[R13,# – 4]! on stack. LDR R0,N Push n – STR R0,[R13,#
Computer Architecture CST 250
X86 Assembly Review.
Chapter 5 Arithmetic and Logic Instructions
CNET 315 Microprocessor & Assembly Language
CS501 Advanced Computer Architecture
Chapter 8: Instruction Set 8086 CPU Architecture
CS-447– Computer Architecture M,W 10-11:20am Lecture 5 Instruction Set Architecture Sep 12th, 2007 Majd F. Sakr
Principles of Computers 18th Lecture
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Presentation transcript:

Pertemuan 7 Bahasa Rakitan: III Matakuliah : T0324 / Arsitektur dan Organisasi Komputer Tahun : 2005 Versi : 1 Pertemuan 7 Bahasa Rakitan: III

Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mendemonstrasikan penggunaan bahasa rakitan dalam instruksi mesin ( C3 ) ( No TIK : 3 )

Assembly Language: III Chapter 2. Assembly Language: III (OFC1)

Figure 2.33. A program for computing the dot product of two vectors. Mo v e #A VEC,R1 R1 p oin ts to v ector A. Mo v e #BVEC,R2 R2 p oin ts to v ector B. Mo v e N,R3 R3 serv es as a coun ter. Clear R0 R0 accum ulates the dot pro duct. LOOP Mo v e (R1)+,R4 Compute the pro duct of Multiply (R2)+,R4 next comp onen ts. Add R4,R0 Add to previous sum. Decremen t R3 Decremen t the coun ter. Branc h > LOOP Loop again if not done. Mo v e R0,DOTPR OD Store dot pro duct in memory . Figure 2.33. A program for computing the dot product of two vectors.

for (j = – n 1; j > 0; j = – j 1) { for ( k = – j 1; k > = 0; k = k – 1 ) { if (LIST[ k ] > LIST[ j ]) { TEMP = LIST[ k ]; LIST[ k ] = LIST[ j ]; LIST[ j ] = TEMP; } } } (a) C-language program for sorting Mo v e #LIST,R0 Load LIST in to base register R0. Mo v e N,R1 Initialize outer lo op index Subtract #1,R1 register R1 to j = n – 1. OUTER Mo v e R1,R2 Initialize inner lo op index Subtract #1,R1 register R2 to k = j – 1. Mo v eByte (R0,R1),R3 Load LIST( j ) in to R3, whic h holds current maxim um in sublist. INNER CompareByte R3,(R0,R2) If LIST( k )  [R3], Branc  h NEXT do not exhange. Mo v eByte (R0,R2),R4 Otherwise, exchange LIST( k ) Mo v eByte R3,(R0,R2) with LIST( j ) and load Mo v eByte R4,(R0,R1) new maxim um in to R3. Mo v eByte R4,R3 Register R4 serv es as TEMP . NEXT Decremen t R2 Decremen t index registers R2 and Branc h  INNER R1, whic h also serv e Decremen t R1 as lo op coun ters, and branc h Branc h > OUTER bac k if lo ops not finished. (b) Assembly language program for sorting Figure 2.34. A byte-sorting program using a straight-selection sort.

Figure 2.35. Linked-list data structure. Link address Record 1 Record 2 Record k Head T ail (a) Linking structure Record 1 Record 2 Ne w record (b) Inserting a new record between Record 1 and Record 2 Figure 2.35. Linked-list data structure.

Figure 2.35. Linked-list data structure. Link address Record 1 Record 2 Record k Head T ail (a) Linking structure Record 1 Record 2 Ne w record (b) Inserting a new record between Record 1 and Record 2 Figure 2.35. Linked-list data structure.

Memory Key Link Data address field field field (ID) (Test scores) 1 word 1 word 3 words First record 2320 27243 1040 Head Second record 1040 28106 1200 Third record 1200 28370 2880 • Second last record 2720 40632 1280 Last 1280 47871 Tail record Figure 2.36. A list of student test scores organized as a linked list in memory.

INSER TION Compare #0, RHEAD Branch>0 HEAD ne w record Mo v e RNEWREC, RHEAD becomes a not empty Return one-entry list HEAD Compare (RHEAD), (RNEWREC) Branch>0 SEARCH insert ne w record Mo v e RHEAD, 4(RNEWREC) ne w record becomes some where after Mo v e RNEWREC, RHEAD ne w head current head Return SEARCH Mo v e RHEAD, RCURRENT LOOP Mo v e 4(RCURRENT), RNEXT Compare #0, RNEXT Branch=0 T AIL ne w record becomes ne w tail Compare (RNEXT), (RNEWREC) Branch<0 INSER T insert ne w record in Mo v e RNEXT , RCURRENT an interior position Branch LOOP INSER T Mo v e RNEXT , 4(RNEWREC) T AIL Mo v e RNEWREC, 4(RCURRENT) Return Figure 2.37. A subroutine for inserting a new record into a linked list.

Figure 2.38. A subroutine for deleting a record from a linked list. DELETION Compare (RHEAD), RIDNUM Branch>0 SEARCH Mo v e 4(RHEAD), RHEAD not the head record Return SEARCH Mo v e RHEAD, RCURRENT LOOP Mo v e 4(RCURRENT), RNEXT Compare (RNEXT), RIDNUM Branch=0 DELETE Mo v e RNEXT , RCURRENT Branch LOOP DELETE Mo v e 4(RNEXT), R TEMP Mo v e R TEMP , 4(RCURRENT) Return Figure 2.38. A subroutine for deleting a record from a linked list.

Figure 2.39. Encoding instructions into 32-bit words. 8 7 7 10 OP code Source Dest Other info (a) One-word instruction OP code Source Dest Other info Memory address/Immediate operand (b) Two-word instruction OP code R i R j R k Other info (c) Three-operand instruction Figure 2.39. Encoding instructions into 32-bit words.

T able 2.1 Generic addressing modes Name Assem bler syn tax Addressing function Immediate #V alue Op erand = V alue Register R i EA = R i Absolute (Direct) LOC EA = LOC Indirect (R i ) EA = [R i ] (LOC) EA = [LOC] Index X(R i ) EA = [R i ] + X Base with index (R i ,R j ) EA = [R i ] + [R j ] Base with index X(R i ,R j ) EA = [R i ] + [R j ] + X and offset Relative X(PC) EA = [PC] + X Autoincremen t (R i )+ EA = [R i ] ; Incremen t R i Autodecrement  (R i ) Decremen t R i ; EA = [R i ] EA = effectiv e address V alue = a signed n um b er

Pertemuan 8 Bahasa Rakitan: IV Matakuliah : T0324 / Arsitektur dan Organisasi Komputer Tahun : 2005 Versi : 1 Pertemuan 8 Bahasa Rakitan: IV

Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mendemonstrasikan penggunaan bahasa rakitan dalam instruksi mesin ( C3 ) ( No TIK : 3 )

Chapter 2. Assembly Language: IV (OFC2)

# include < stdio.h > v oid main(void) { long NUM1[5]; long SUM; long N; NUM1[0] = 17; NUM1[1] = 3; NUM1[2] =  51; NUM1[3] = 242; NUM1[4] = 113; SUM = 0; N = 5; asm { LEA EBX,NUM1 MO V ECX,N MO V EAX,0 MO V EDI,0 ST AR T ADD: ADD EAX,[EBX + EDI*4] INC EDI DEC ECX JG ST AR T ADD MO V SUM,EAX } printf ("The sum of the list v alues is %ld \ n", SUM ); } Figure D.2. IA-32 Program in Figure 3.40a encapsulated in a C/C++ program.

Machine instructions Assembly language instructions (hexadecimal) 03 04 BB STARTADD: ADD EAX,[EBX + EDI*4] 47 INC EDI 49 DEC ECX 7F F9 JG ST AR T ADD (a) Loop body encoding OP code ModR/M byte SIB byte 03 04 BB 00000011 00 000 100 10 111 011 ADD (see T able D.2) (see Figure D.1 c ) (doubleword (b) ADD instruction OP code Offset 7F F9 01111111 111111001 JG 7 (short offset) (c) JG instruction Figure D.3. Encoding of the loop body in Figure D.2.

T ABLE D.1 Register field enco ding in IA-32 instructions Reg/Base/Index* Register field EAX 1 ECX 1 EDX 1 1 EBX 1 ESP 1 1 EBP 1 1 ESI 1 1 1 EDI *ESP (100) cannot be used as an index register.

IA-32 addressing modes selected by the ModR/M and SIB bytes ModR/M b ABLE D.2 IA-32 addressing modes selected by the ModR/M and SIB bytes ModR/M b yte Addressing mode Mod R/M field field b b b b b 7 6 2 1 Reg Register indirect EA = [Reg] 1 Reg Base with 8-bit displacement EA = [Reg] + Disp8 1 Reg Base with 32-bit displacement EA = [Reg] + Disp32 1 1 Reg Register EA = Reg Exceptions 1 1 Direct EA = Disp32 1 Base with index (uses SIB b yte) EA = [Base] + [Index]  Scale When Base = EBP the addressing mo de is: Index with 32-bit displacement EA = [Index]  Scale + Disp32 1 1 Base with index and 8-bit displacement (uses SIB b yte) EA = [Base] + [Index]  Scale + Disp8 1 1 Base with index and 32-bit displacemet (uses SIB b yte) EA = [Base] + [Index]  Scale + Disp32

T ABLE D.3 Scale field encoding in IA-32 SIB b yte Scale field Scale 1 1 2 1 4 1 1 8

^ Table D.4 – page 1 T ABLE D.4 IA-32 instructions Mnemonic Size Operands Operation CC flags (Name) performed affected dst src S Z O C ADC B,D reg reg dst  [dst] + [src] + [CF] x x x x (Add with reg mem carry) mem reg reg imm mem imm ADD B,D reg reg dst  [dst] + [src] x x x x (Add) reg mem mem reg reg imm mem imm AND B,D reg reg dst  [dst] ^ [src] x x (Logical reg mem AND) mem reg reg imm mem imm BT D reg reg bit# = [src]; x (Bit test) reg imm8 CF  bit# of [dst] mem reg mem imm8 BTC D reg reg bit# = [src]; x (Bit test and reg imm8 CF  bit# of [dst]; complement mem reg complement bit# mem imm8 of [dst] BTR D reg reg bit# = [src]; x (Bit test reg imm8 CF  bit# of [dst]; and reset) mem reg clear bit# of [dst] to mem imm8 Table D.4 – page 1

             Table D.4 – page 2 T ABLE D.4 ( Continued ) Mnemonic Size Operands Operation CC flags (Name) performed affected dst src S Z O C BTS D reg reg bit# = [src]; x (Bit test reg imm8 CF  bit# of [dst]; and set) mem reg set bit# of [dst] to 1 mem imm8  CALL D reg ESP [ESP] – 4; (Subroutine mem [ESP]  [EIP];  call) EIP EA of dst  CLC CF (Clear carry)  CLI IF (Clear int. flag) CMC CF  [CF] x (Compl. carry) CMP B,D reg reg [dst]  [src] x x x x (Compare) reg mem mem reg reg imm mem imm DEC B,D reg dst  [dst] – 1 x x x (Decrement) mem DIV B,D reg for B: ? ? ? ? (Unsigned mem [AL]/[src]; divide) AL  quotient; AH  remainder for D: [EAX]/[src]; EAX  quotient; ED X  remainder Table D.4 – page 2

             Table D.4 – page 3 T ABLE D.4 ( Continued ) Mnemonic Size Operands Operation CC flags (Name) performed affected dst src S Z O C HL T Halts execution un til (Halt) reset or external in terrupt o ccurs IDIV B,D reg for B: ? ? ? ? (Signed mem [AL]/[src]; divide) AL  quotient; AH  remainder for D: [EAX]/[src]; EAX  quotient;  ED X remainder IMUL B,D reg (double-length product) ? ? x x (Signed mem for B: m ultiplication) AX  [AL]  [src] for D: ED X,EAX  [EAX]  [src] D reg reg (single-length pro duct) ? ? x x reg mem reg  [reg]  [src] IN B,D dst = AL AL or EAX  [src] (Isolated or EAX input) src = imm8 or [D X] INC B,D reg dst  [dst] + 1 x x x (Increment ) mem INT D imm8 Push EFLA GS; (Software Push EIP; in terrupt) EIP  address (determined b y imm8) Table D.4 – page 3

^ ^ T ABLE D.4 ( Continued) Mnemonic Size Operands Operation CC flags (Name) performed affected dst src S Z O C IRET D P op EIP; x x x x (Return from P op EFLA GS in terrupt) LEA D reg mem reg  EA of src (Load effectiv e address) LOOP D target ECX  [ECX] – 1; (Lo op) If ( [ECX]  ) EIP  target LOOPE D target ECX  [ECX] – 1; (Lo op on If ( [ECX]  equal/zero) ^ [Z] = 1 )  EIP target LOOPNE D target ECX  [ECX] – 1; (Lo op on If ( [ECX]  not equal/ ^ [Z]  1 ) not zero) EIP  target MO V B,D reg reg dst  [src] (Mo v e) reg mem mem reg reg imm mem imm MO VSX B reg reg reg  sign extend [src] (Sign extend reg mem b yte in to register) Table D.4 – page 4

          Table D.4 – page 5 T ABLE D.4 ( Continued) Mnemonic Size Operands Operation CC flags (Name) performed affected dst src S Z O C MO VZX B reg reg reg  zero extend [src] (Zero extend reg mem b yte in to register) MUL B,D reg (double-length pro duct) ? ? x x (Unsigned mem for B: m ultiplication) AX  [AL]  [src] for D: ED X,EAX  [EAX]  [src] NEG B,D reg dst  2's-complement x x x x (Negate) mem [dst] NOP alias for: (No op eration) X CHG EAX,EAX NOT B,D reg dst  [dst ] (Logical mem complement) OR B,D reg reg dst  [dst]  [src] x x (Logical OR) reg mem mem reg reg imm mem imm OUT B,D dst = imm8 dst  [AL] or [EAX] (Isolated or [D X] output) src = AL or EAX Table D.4 – page 5

        Table D.4 – page 6 T ABLE D.4 ( Continued) Mnemonic Size Operands Operation CC flags (Name) performed affected dst src S Z O C POP D reg dst  [[ESP]]; (Pop off mem ESP  [ESP] + 4 stack) POPAD D Pop eight doublewords (Pop off off stack in to stack in to EDI, ESI, EBP , discard, all registers EBX, EDX, ECX, EAX; except ESP) ESP  [ESP] + 32 PUSH D reg ESP  [ESP] – 4; (Push on to mem [ESP]  [src] stac k) imm PUSHAD D Push contents of (Push all EAX, ECX, EDX, EBX, registers ESP , EBP , ESI, EDI on to stack) on to stack; ESP  [ESP] – 32 R CL B,D reg imm8 See Figure 2.32 b ; ? x (Rotate left reg CL src operand is with C flag) mem imm8 rotation count mem CL R CR B,D reg imm8 See Figure 2.32 d ; ? x (Rotate righ t reg CL src operand is with C flag) mem imm8 rotation count mem CL  RET EIP [[ESP]]; (Return from ESP  [ESP] + 4 subroutine) Table D.4 – page 6

T ABLE D.4 (Continued) Mnemonic Size Operands Operation CC flags (Name) performed affected dst src S Z O C R OL B,D reg imm8 See Figure 2.32 a ; ? x (Rotate left) reg CL src operand is mem imm8 rotation count mem CL R OR B,D reg imm8 See Figure 2.32 c ; ? x (Rotate righ t) reg CL src operand is mem imm8 rotation count mem CL SAL B,D reg imm8 See Figure 2.30 a ; x x ? x (Shift reg CL src operand is arithmetic mem imm8 shift count left) mem CL same as SHL SAR B,D reg imm8 See Figure 2.30 c ; x x ? x (Shift reg CL src operand is arithmetic mem imm8 shift count right) mem CL SBB B,D reg reg dst  [dst] – [src] x x x x (Subtract reg mem – [CF] with b orrow) mem reg reg imm mem imm SHL B,D reg imm8 See Figure 2.30 a ; x x ? x (Shift reg CL src operand is left) mem imm8 shift count same as SAL mem CL Table D.4 – page 7

^ T ABLE D.4 (Continued) Mnemonic Size Operands Operation CC flags (Name) performed affected dst src S Z O C SHR B,D reg imm8 See Figure 2.30 b ; x x ? x (Shift reg CL src operand is righ t) mem imm8 shift coun t mem CL STC CF  1 1 (Set carry flag) STI IF  1 (Set in terrupt flag) SUB B,D reg reg dst  [dst] – [src] x x x x (Subtract) reg mem mem reg reg imm mem imm TEST B,D reg reg [dst] ^ [src]; x x (T est) mem reg set flags based reg imm on result mem imm X CHG B,D reg reg [reg]  [src] (Exchange) reg mem X OR B,D reg reg dst  [dst]  [src] x x (Exclusive reg mem OR) mem reg reg imm mem imm Table D.4 – page 8

    T ABLE D.5 IA-32 conditional jump instructions Mnemonic code name test JS Sign (negative) SF = 1 JNS No sign (positive or zero) SF = JE/JZ Equal/Zero ZF = 1 JNE/JNZ Not equal/Not zero ZF = JO Overflow OF = 1 JNO No overflow OF = JC/JB Carry/Unsigned below CF = 1 JNC/JAE No carry/Unsigned above or equal CF =  JA Unsigned above CF ZF =  JBE Unsigned below or equal CF ZF = 1 JGE Signed greater than or equal SF  OF = JL Signed less than SF  OF = 1 JG Signed greater than ZF  (SF  OF) =  JLE Signed less than or equal ZF (SF  OF) = 1