Computer Architecture 2 PDF
Document Details
Uploaded by RicherLotus
2024
Mr. A. LAHMISSI
Tags
Summary
This document is an introduction to computer architecture, focusing on microprocessor language and assembly language. It covers topics like high-level languages, machine language, and addressing modes.
Full Transcript
Course Document Computer Architecture Mr. A. LAHMISSI - November 2024 1 X R 1 Introduction 2 Assembler language - Machine language, high-level language 3 Assembler language - Addressi...
Course Document Computer Architecture Mr. A. LAHMISSI - November 2024 1 X R 1 Introduction 2 Assembler language - Machine language, high-level language 3 Assembler language - Addressing modes 4 Assembler language - Common instructions 5 Assembler language - Program Structure 6 Assembler language - Practices 7 Summery and Conclusions 8 9 2 X R Assembly language High/Low level languages Machine language Assembly language Instruction’s Addressing modes Common instructions Transfer instructions Arithmetic instructions Logical instructions Jump/loop instructions Program Structure Assembler Editors variables Subprogram macro Interruptions Summery Practice Common mistakes 3 H Assembly language 4 R H Assembly language Assembly language concept Assembly is a programming language that transform its instruction text into tasks codes that microprocessor can understand and perform. The task codes are difficult for humans to understand, so the assembler was made for humans to facilitate programming microprocessor tasks and instructions. There many microprocessors and each microprocessor has its specific dedicated Assembly language different from the others but all Assembly language are very similar to each others. Example 1: Motorola 6800 programming language for the 6800 microprocessor. Example 2: Intel 8086 programming language for the 8086 microprocessor. Assembly language simulators and editor In order to fill the intended Assembly program properly, editor and emulators are developed to help human writing and developing programs. There are many emulators depending on the used microprocessor. 5 R H High-level language Assembly language Machine language High-level language Low level closest to the Lowest Level (for human) machine (for human) (Machine 0/1) Program written in Program written in H-L language Assembly language Machine code 0011101010100 int Number=5; MOV AX, BX MOV CX, N 1011001010001 C String=‘’abc’’; Scanner scan; ADD DX, 0020h 0101001010100 P MUL BL 1010101000000 System.out.prit; DIV BL 00111110101011 U Compiler assembleur MOV AL, a 0011101010100 SHL AL, 1 1011001010001 C MOV BL, b P:=(a*2)+(b*2); SHL BL, 1 0101001010100 P 1010101000000 ADD AL, BL MOV P, AL 00111110101011 U 6 R H Assembly language Machine language MACHINE INSTRUCTION It is the only language that can be executed directly by the microprocessor. This language is difficult to master since each instruction is coded by a specific sequence of bits (Binary Digit). Field Depends on Depends on Depends on required The instruction The instruction The instruction 7 0 7 0 7 0 7 0 7 0 7 0 Op.Code + Addressing mode Dept bas Dept haut Data bas Data haut Dept. Bas: low byte of the displacement cited in the addressing mode of the memory operand Dept. Haut: high byte of the displacement cited in the addressing mode of the memory operand Data. Bas: low byte of the immediate data cited in the instruction Data. Haut: high byte of the immediate data cited in the instruction 7 R H Assembly language Machine language MACHINE INSTRUCTION Field Depends on Depends on Depends on required The instruction The instruction The instruction 7 0 7 0 7 0 7 0 7 0 7 0 Op.Code D W MOD REG R/M Dept bas Dept haut Data bas Data haut D (Destination): Indicates whether the register is a source (D=0) or destination (D=1) W (Width): Indicates whether the operand is 8 (W=0) bits or 16 bits (W=1) [D, REG] is a pair that works together [MOD, R/M] is a pair that works together 8 R H Example : MOV instruction Op.Code D W MOD REG R/M Dept bas Dept haut Data bas Data haut MOV R(M), R(M) 100010DW MOD REG R/M Depl B Depl H MOV R(M), Data 1100011W MOD 000 R/M Depl. B Depl. H Data. B Data. H MOV R, Data 1011 W REG Data. B Data. H MOV Acc, M 1010 000W Depl. B Depl. H MOV M, Acc 1010 001W Depl. B Depl. H MOV SR:, R(M) 1000 1110 MOD 0SR R/M Depl. B Depl. H MOV R(M), R:S 1000 1100 MOD 0SR R/M Depl. B Depl. H R: Registre SR: Segment Registre (ES, SS, DS, CS) MOD: Addressing mode 9 R H MOV R, Data 1011 W REG Data. B Data. H MOV Acc, M 1010 000W Depl. B Depl. H 1010 0000 0100 1000 0110 0110 = A0 48 66 Size of instruction = 3 Bytes ADD Acc, Data 0000 010W Data. B Data. H 0000 0101 0011 0100 0001 0010 = 05 34 12 Example: ADD AX, 1234H Size of instruction = 3 Bytes 10 R H Assembly language Machine language Example: MOV BL, [SI] MOV R(M), R(M) 100010DW MOD REG R/M Depl B Depl H D (Destination): D=1; destination registre 1000 1010 MOD REG R/M D=0; source registre W (Width): W=1; argument 16 bits 1000 1010 MOD 011 R/M W=0; argument 8 bits MOV BL, Reg8.(Mem.8) [D,REG] 1000 1010 00 011 100 REG=011 MOV BL, [SI] [MOD,R/M] MOD=00 (no displacement) R/M=100 ([SI]) 8A 1C MOV Reg.8, Reg8.(Mem.8) 11 R H Assembly language Machine language Exemple: MOV AL, usthb MOV R(M), R(M) 100010DW MOD REG R/M Depl B Depl H Size of instruction = 4 octets MOV Acc, M 1010 000W Depl. B Depl. H Size of instruction = 3 octets 1010 0000 0100 1000 0110 0110 = A0 48 66 12 H Assembly language Assembly language is the closest to machine language It is Symbolic It has logical & arithmetic instructions And also has, Loading, transfer instructions, …etc. Each instruction represents a machine code ASSEMBLY INSTRUCTION Symbol operation Operand2, Oprerand1 Destination Source Exemple Symbol operation operands ADD Op2, Op1 MOV Op2, Op1 NOT Op 13 R H Assembly language INSTRUCTION instruction code = Operator code Operands Arithmetical Logical (+, -, %, *…) (AND, OR, XOR, …) Exemple Instruction1: ADD A, B // (ADD): Binary Operator (A,B): Operands Instruction2: NOT A // (NOT): Unary Operator (A): Operand Operand types: REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP. SREG: DS, ES, SS, and only as second operand: CS. Memory: [BX], [BX+SI+7], variable, etc...( Memory Access). Immediate: 5, -24, 3Fh, 10001101b, etc... 14 R H Assembly language CPU Registers 15 H Assembly language CPU Registers The PSW Register – - (the FLAGS) Status Bits (Red) These are readable only bits, affected by some instructions. CF (Carry-out): set to 1 in case of overflow on unsigned operation OF (Overflow): set to 1 in case of overflow on signed operation. ZF (Zero): set to 1 if the result of the operation is 0. PF (Parity): 1 if the low-order of result contains even number of 1, 0 otherwise. AF (Auxiliary): set to 1, if there is a carry on the 3rd bit, 0 otherwise SF (Sign): set to 1, if the MSB bit of the result is = 1, 0 otherwise 16 H Assembly language CPU Registers The PSW Register – - (the FLAGS) 15 0 X X X X OF DF IF TF SF ZF X AF X PF X CF PSW DF: Direction Flag, IF: interruption Flag, TF: Trap Flag Control Bits (bleu) These are writeable bits, configured by the programmer. DF=0: SI, DI will be incremented by concerned instructions (instruction : CLD // DF=0) DF=1: SI, DI will be decremented by concerned instructions (instruction : STD // DF=1) IF=0 : interruptions ignored (instruction : CLI // IF=0) IF=1 : interruptions allowed (instruction : STI // IF=1) TF=0: step-by-step execution TF=1: block execution To modify the bit TF: PUSHF //(empiler le registre PSW dans la pile) POP AX // mettre la pile en AX et donc AX=PSW OR AX, 0000000100000000b (Pour TF=1) // ou AND AX, 1111111011111111b (for TF=0) PUSH AX POPF // (Dépiler sommet de pile vers PSW) 17 H Addressing modes 18 H Addressing Modes Register addressing This mode uses the CPU internal registers (Source: register, Destination: register) Examples MOV AX, BX AX SI BX DI CX SP DX BP CPU SS IP CS PSW DS ES 19 H Addressing Modes Immediate addressing In this addressing mode the operand (as value) appears in the instruction itself Examples MOV AX, 5 AX 5 SI BX DI CX SP DX BP CPU SS IP CS PSW DS ES 20 H Addressing Modes Direct addressing In this addressing mode, unlike immediate addressing mode, the operand contains the address of the data in memory Examples MOV AX, [1234H] Memory 100 AX SI BX DI CX SP DX BP DS 1234H 100 CPU SS IP CS PSW DS ES 21 H Addressing Modes Register Indirect Addressing (Based) Indirect addressing mode allows data to be accessed through a base register. Examples MOV AX, [BX] Mémoire 500 AX SI 2000H BX DI CX SP DX BP DS 2000H 500 CPU SS IP CS PSW DS ES 22 H Addressing Modes Register Indirect Addressing (Based) Indirect addressing mode allows data to be accessed through a base register. Examples MOV AX, ES:[BX] Mémoire 200 AX SI 2000H BX DI CX SP DX BP DS 2000H 500 CPU SS IP CS PSW 2000H 200 DS ES ES 23 H Addressing Modes Register Indirect Addressing (Based) + dep Indirect addressing mode allows data to be accessed via a base register with displacement Examples MOV AX, [BX+0002H] Mémoire 100 AX SI 2000H BX DI CX SP DX BP DS 2002H 2000H 100 500 CPU SS IP CS PSW DS ES 24 H Addressing Modes Register Indirect Addressing (Based) Indirect addressing mode allows data to be accessed through a pointer register. Examples MOV AX, [BP] Mémoire 250 AX SI 1000H BX DI SS 3000H 250 CX SP DX BP 3000H DS 2000H 500 CPU SS IP CS PSW 1000H 200 DS ES ES 25 H Addressing Modes Indexed addressing Similar to based addressing, but, it uses the SI and DI index registers Examples MOV AX, [SI] 400 AX SI 1500H Mémoire 2000H BX DI CX SP DX BP DS 2002H 2000H 100 500 CPU 1500H 400 SS IP CS PSW DS ES 26 H Addressing Modes Indexed addressing Similar to based addressing, but, it uses the SI and DI index registers with displacement Examples Indexed Addressing + Dep MOV AX, [DI+0200H] Mémoire 300 AX SI 2000H BX DI 1500H CX SP DX BP DS 100 500 CPU 1700H 300 1500H SS IP CS PSW DS ES 27 H Addressing Modes Indexed addressing Index Based Addressing It is an addressing mode that combines both modes (based and indexed) Examples MOV AX, [BX+SI] 500 AX SI 500H Mémoire 1000H BX DI 1500H CX SP DX BP DS 1500H 100 500 CPU 300 SS IP CS PSW DS ES 28 H Addressing Modes Indexed addressing Index Based Addressing It is an addressing mode that combines both modes (based and indexed) with displacement Examples MOV AX, [BX+SI+0200H] 100 AX SI 500H Mémoire 1000H BX DI 1500H CX SP DX DI BP DS 1700H 1500H 100 500 CPU 300 SS IP CS PSW DS ES 29 H Addressing Modes Indexed addressing Index Based Addressing It is an addressing mode that combines both modes (pointer and indexed) Examples MOV AX, [BP+SI] Mémoire 350 AX SI 500H 1000H BX DI 1500H SS 1500H 350 CX SP DX BP 1000H DS 1700H 1500H 100 500 CPU 300 SS IP CS PSW DS ES 30 H Addressing Modes Indexed addressing Index Based Addressing It is an addressing mode that combines both modes (pointer-based and indexed) with displacement Indexed Based Addressing (+Dep) Examples MOV AX, [BP+SI+0200H] Mémoire 450 AX SI 500H 1700H 450 1000H BX DI 1500H SS 350 CX SP DX BP 1000H DS 1700H 1500H 100 500 CPU 300 SS IP CS PSW DS ES 31 H Addressing Modes Register addressing MOV AX, BX Immediate addressing MOV AX, 5 Direct addressing MOV AX, [1234H] Register Indirect Addressing (Based) MOV AX, [BX] MOV AX, ES:[BX] MOV AX, [BX+0002H] MOV AX, [BP] Indexed addressing MOV AX, [SI] MOV AX, [DI+0200H] Index Based Addressing MOV AX, [BX+SI] MOV AX, [BX+SI+0200H] MOV AX, [BP+SI] MOV AX, [BP+SI+0200H] 32 H Addressing Modes INTERDICTIONS !! ⋉ MOV Val_1, Val_2 ⋉ MOV AL, BX ⋉ MOV DS, ES ⋉ MOV DS, 1000H ⋉ MOV Mot_1, Mot_2 ⋉ Immediate values can never be assigned to segment registers. 33 H Addressing Modes Addressing Mode Operande Segment Register AX, BX, … // Immediat Data (Val) // Direct [Offset] DS [BX] / [BX+Val] DS ou ES Based / Based with displacement [BP] / [BP+Val] SS Indexed [SI] or [DI] DS Indexed with displacement [SI + Val] or [DI + Val] DS [BX+SI] or [BX+DI] DS Based indexed [BP+SI] or [BP+DI] SS [BX+SI+ Val] or [BX+DI+ Val] DS Based indexed with displacement [BP+SI+ Val] or [BP+DI+ Val] SS Implicit (Loop ‘’CX’’) // 34 R H Accumulator (AX) Base (BX) Counter (CX) Data (DX) Code Segment (CS) Data Segment (DS) Stack Segment (SS) Extra Segment (ES) Source Index (SI) Destination Index (DI) Base Pointer (BP) Stack Pointer (SP) Instruction Pointer (IP) Carry flag (CF) Parity flag (PF) Auxiliary flag (AF) Zero flag (ZF) Sign flag (SF) Overflow flag (OF) Interrupt flag (IF) Direction flag (DF) Trap flag (TF) 35 R H EU (Execution Unit) BIU (Bus Interface Unit) both work simultaneously. BIU will access memory and EU executes the instruction fetched by BIU. 36 R H EU (Execution Unit) Register Explanation : General-purpose registers, Special purpose registers: are 4 registers, primarily used for There are two index registers and three pointer registers. arithmetic and data movement Source Index (SI) Accumulator (AX) Destination Index (DI) Base (BX) Base Pointer (BP) Counter (CX) Stack Pointer (SP) Data (DX) Instruction Pointer (IP) six segment registers, Code Segment (CS) Data Segment (DS) Stack Segment (SS) Extra Segment (ES) a processor status flags register (EFLAGS), and an instruction pointer (EIP). 37 R H some general purpose register EAX = Extended accumulator EBX = Extended Base ECX = Extended Counter EDX = Extended Data register EAX = Extended Accumulator 38 R H EAX (extended accumulator register) is automatically used by multiplication and division instructions. For example, in multiplication operation, one operand is stored in EAX or AX or AL register according to the size of the operand. EBX (Extended Base Counter) : is used for addressing, particularly when dealing with arrays and strings and it also can be used as a data register when not used for addressing. ECX (Extended Counter): ECX or CX are used as a loop counter. EDX (Extended data register) : is used for In and Out instructions (input, output), also used to store partial results of Mul and Div operations and in other cases, can be used as a data register. 39 R H MOV AX, BX MOV AX, 5 MOV AX, [1234H] MOV AX, [BX] ESI and EDI (Extended Source Index) and MOV AX, ES:[BX] (Extended Destination Index registers) MOV AX, [BX+0002H] are used by high-speed memory transfer instructions. MOV AX, [BP] EBP (extended frame pointer register) is used by high-level MOV AX, [SI] languages to reference function parameters and local MOV AX, [DI+0200H] variables on the stack. It should not be used for ordinary MOV AX, [BX+SI] arithmetic or data transfer except at an advanced level of programming. MOV AX, [BX+SI+0200H] MOV AX, [BP+SI] ESP (Extended Stack Pointer) : addresses data on the stack MOV AX, [BP+SI+0200H] (a memory segment). It is rarely used for ordinary arithmetic or data transfer. 40 R H AX = Extended accumulator BX = Extended Base CX = Extended Counter CX : Counter used by some instructions DX = Extended Data register (SI) Source Index Code Segment (CS) (DI) Destination Index Data Segment (DS) (BP) Base Pointer (BP generally Used to address Stack DATA SS:[BP]) Stack Segment (SS) (SP) Stack Pointer Extra Segment (ES) Notice: (IP) Instruction Pointer Memory Cell=1B Byte XLATB Copy value of memory byte at DS:[BX + unsigned AL] to AL register. Byte Byte STOSW Store word in AX into ES:[DI]. Update SI... LODSB Load byte at DS:[SI] into AL. Update SI.. XLATB Byte Byte 41 H Common instructions 42 H Assembly language High/Low level languages Machine language Assembly language Instruction’s Addressing modes Common instructions Transfer instructions MOV // load, store Arithmetic instructions ADD, SUB, … Logical instructions AND, OR, Jump/loop instructions JMP, // jumps and branches Program Structure Assembler Editors variables Subprogram macro Interruptions Summery Practice Common mistakes 43 H Common instructions Transfer instructions Syntax: MOV Destination, Source This instruction copies a byte (8-bit) or a word (16-bit) from source to destination. Both operands should be of Examples the same type (byte or word). MOV AX, BX Register to register MOV AX, 5 Register to memory MOV AX, [1234H] Memory to register MOV AX, [BX] MOV AX, ES:[BX] MOV AX, [BX+0002H] MOV AX, [BP] MOV AX, [SI] MOV AX, [DI+0200H] MOV AX, [BX+SI] MOV AX, [BX+SI+0200H] MOV AX, [BP+SI] MOV AX, [BP+SI+0200H] 44 R H LDS , LES Load memory double word into word register and DS/ES. ORG 100h ; command in asm editor only Algorithm: LDS AX, m ; AX =1234h, DS = 0800h. REG = first word DS = second word LES AX, n ; AX =1234h, ES = 1000h. RET m DW 1234h DW 0800h n DW 1234h DW 1000h END LEA ORG 100h Algorithm: LEA AX, m REG = address of memory RET (offset) m DW 1234h END ;AX is set to: 0104h. LEA instruction takes 3 bytes, RET takes 1 byte, we start at 100h, so the address of 'm' is 104h. 45 R H Algorithm: SHL Shift operand1 Left. The number of shifts is set by operand2. Shift all bits left, the bit that goes off is set to CF. MOV AL, 11100000b Zero bit is inserted to the SHL AL, 1 ; AL = 11000000b, CF=1. right-most position. SHR Shift operand1 Right. The number of shifts is set by operand2. Algorithm: Shift all bits right, the bit MOV AL, 00000111b that goes off is set to CF. SHR AL, 1 ; AL = 00000011b, CF=1. Zero bit is inserted to the left-most position. OR Logical OR between all bits of two operands. Result is stored in first operand. Algorithm: MOV AL, 41h ; AL = 01000001b (41h) 1 OR 1 = 1 OR AL, 00100000b ; AL = 01100001b (61h) 1 OR 0 = 1 0 OR 1 = 1 0 OR 0 = 0 AND Logical AND between all bits of two operands. Result is stored in operand1. Algorithm: 1 AND 1 = 1 MOV AL, 61h ; AL = 01100001b (61h) 1 AND 0 = 0 AND AL, 11011111b ; AL = 01000001b (41h) 0 AND 1 = 0 0 AND 0 = 0 46 H Common instructions Addition (ADD) and Subtraction (SUB): ADD adds the data of the destination and source operand and stores the result in destination. Both operands should be of the same type (words or bytes), otherwise, the assembler will generate an error. The subtraction instruction subtracts the source from destination and stores the result in destination. 47 R H ADD operand1 = operand1 + operand2 MOV AL, 5 SUB AL, 1 ; AL = 4 SUB operand1 = operand1 - operand2 MOV AL, 5 ADD AL, -3 ; AL = 2 XOR Result is stored in first operand. MOV AL, 00000111b XOR AL, 00000010b ; AL = 00000101b Exercise [Answers] 48 R H MUL Unsigned multiply. Algorithm: MOV AL, 200 ; AL = 0C8h when operand is a byte: MOV BL, 4 AX = AL * operand. MUL BL ; AX = 0320h (800) when operand is a word: RET (DX AX) = AX * operand. Algorithm: DIV Unsigned divide. when operand is a byte: MOV AX, 203 ; AX = 00CBh AL = AX / operand MOV BL, 4 AH = remainder (modulus) DIV BL ; AL = 50 (32h), AH = 3 when operand is a word: AX = (DX AX) / operand RET DX = remainder (modulus) Exercise [Answers] Exercise [Answers] 49 R H Exercise What does below program do?. [Answers] ORG 100h ; command in asm editor only Calculate the square value MOV AX,[1000h] of a number filled in the MUL AX memory cell [1000h] RET Exercise What does below program do?. [Answers] ORG 100h ; command in asm editor only Calculate the addition of MOV AX,1010b the following two binary MOV BX,1001b numbers : 1010 et 1001. ADD AX,BX AX= 0001 0011b = 13h Exercise What does below program do?. [Answers] 50 R H PUSH Store 16 bit value in the stack. ORG 100h ; command in asm editor only Algorithm: MOV AX, 1234h SP = SP – 2 PUSH AX POP DX ; DX = 1234h SS:[SP] (top of the stack) = RET operand POP Get 16 bit value from the stack. ORG 100h ; command in asm editor only Algorithm: MOV AX, 1234h operand = SS:[SP] (top of PUSH AX the stack) POP DX ; DX = 1234h RET SP = SP + 2 51 R H Source Memory Any Memory Writing in memory DS:[SI] Any Memory (SI automatically inc/dec ~DF) Writing in memory Reading from memory MOV DS:[SI], AL MOV [1234H], AX MOV AX, [1234H] MOV ES:[DI], AL MOV [BX], 5 MOV AX, [BX] Destination Memory MOV ES:[BX], AX MOV AX, ES:[BX] MOV DS:[BX], AX MOV AX, DS:[BX] Writing in memory ES:[DI] MOV [BX+0002H], AX MOV AX, [BX+0002H] (DI automatically inc/dec ~DF) MOV [BP], AX MOV AX, [BP] MOV [SI], AX MOV AX, [SI] LES MOV [DI+0200H], AX MOV AX, [DI+0200H] STD MOV [BX+SI], AX MOV AX, [BX+SI] STOSB MOV [BX+SI+0200H], AX MOV AX, [BX+SI+0200H] STOSW MOV [BP+SI], AX MOV AX, [BP+SI] CLD MOV [BP+SI+0200H], AX MOV AX, [BP+SI+0200H] Notice: Memory Cell=1Byte Stack Memory Byte Writing/reading in memory SS:[SP] Byte (SPautomatically inc/dec ~W/R). PUSH. POP Byte 52 R H LEA Load Effective Address. ORG 100h ; command in asm editor only LEA AX, data ; AX is set to: 0104h RET data DW 1234h END ; command in asm editor only ; LEA instruction takes 3 bytes, ; RET takes 1 byte, we start at 100h, so the address of ' data ' is 104h. MOVSB Copy byte at DS:[SI] to ES:[DI]. Update SI and DI (inc or dec ~DF). ORG 100h ; command in asm editor only Algorithm: LEA SI, a1 ES:[DI] = DS:[SI] LEA DI, a2 if DF = 0 then MOV CX, 5 SI = SI + 1 REP MOVSB DI = DI + 1 RET else a1 DB 1,2,3,4,5 SI = SI - 1 a2 DB 5 DUP(0) DI = DI - 1 53 R H XLATB Read value from memory byte at DS:[BX + unsigned AL] to AL. Algorithm: ES:[DI] = AL ORG 100h ; command in asm editor only if DF = 0 then LEA BX, data DI = DI + 1 MOV AL, 2 else XLATB ; AL = DS:[BX + unsigned AL = 33h DI = DI - 1 RET data DB 11h, 22h, 33h, 44h, 55h STOSB Store byte in AL into ES:[DI]. Update SI. Algorithm: ES:[DI] = AL ORG 100h ; command in asm editor only if DF = 0 then MOV AL, 12h DI = DI + 1 STOSB ; store 12h in ES:[DI] eqv MOV ES:[DI],AL else RET DI = DI - 1 STOSW Store word in AX into ES:[DI]. Update SI. Algorithm: ES:[DI] = AX ORG 100h ; command in asm editor only if DF = 0 then MOV AX, 1234h DI = DI + 2 STOSW ; store 1234h in ES:[DI] eqv MOV ES:[DI],AL else RET DI = DI - 2 54 R H LODSB Load byte at DS:[SI] into AL. Update SI. Algorithm: AL = DS:[SI] ORG 100h ; command in asm editor only if DF = 0 then LODSB ; AL = DS:[SI] (SI inc automaticly) SI = SI + 1 RET else SI = SI - 1 DS:[SI] Exercise memory write- memory read [Answers] ES:00 0 ES:01 5 ES:02 0 ORG 100h ; command in asm editor only ES:03 4 MOV CX, 5 ES:04 0 MOV SI, 00h ES:05 3 mw: MOV DS:[SI], CX ; write CX value into DS:[SI] INC SI 0 LOOP mw 2 MOV CX, 5 ES:06 0 Stack MOV SI, 00h ES:07 1 01 mr: LODSB ; AL = DS:[SI] (SI inc automaticly) 07 PUSH AX 02 LOOP mr 07 RET 03 07 04 0755 05 R H INT 10h Screen interface. ORG 100h ; command in asm editor only Algorithm: LEA SI, a1 AL = DS:[SI] MOV CX, 5 if DF = 0 then MOV AH, 0Eh SI = SI + 1 me: LODSB ; AL = DS:[SI] (SI inc automaticly) else INT 10h ; if AH=0Eh, then printScreen AL SI = SI - 1 LOOP me MOV AL, '!' ; INT 10h ; if AH=0Eh, then printScreen AL INT 10h ; if AH=0Eh, then printScreen AL RET a1 DB 'H', 'e', 'l', 'l', 'o' ; 56 R H LOOP Decrease CX, jump to label if CX not zero. Algorithm: CX = CX - 1 ORG 100h ; command in asm editor only if CX ≠0 then jump MOV CX, 5 else Label1: PUSH CX ; 5 times no jump, continue LOOP label1 RET Exercise Find what is filled in the Stack (Cell = 1Byte) and ES:[CX], values. [Answers] ORG 100h ; command in asm editor only ES:[DI] MOV SP, 0300H Stack ES:FFF8h 01h MOV CX, 4 01h ES:FFF9h 00h Lp1: PUSH CX ;PUSH accept 16bit only no CL 00h ES:FFFAh 02h LOOP Lp1 02h ES:FFFBh 00h STD ; Set DF. SI&DI decrement by STOSW. 00h ES:FFFCh 03h MOV CX, 4 03h ES:FFFDh 00h Lp2: POP AX ; AX=1 then 2 then 3 then 4 00h ES:FFFEh 04h STOSW 04h ES:FFFFh 00h LOOP Lp2 00h CLD ; Set DF. SI&DI increment by STOSW. RET 57 R H LOOPNE Decrease CX, jump to label if CX not zero and (ZF = 0). ORG 100h Algorithm: STACK MOV CX, 8 CX = CX - 1 SS:[SP] MOV AX, 5 if (CX ≠ 0) and (ZF = 0) SS:0000h Label1: DEC AX then jump SS:0001h LOOPNE label1 ; 5 times else SS:0002h RET no jump, continue SS:0003h SS:0004h Exercises Find what is filled in the Stack (Cell = 1Byte) and DS:SI. [Answers] SS:0005h ORG 100h ; DS:[SI]...... MOV SI, 0 DS:0000h 09h...... MOV CX, 5 DS:0001h SS:FFF5h 00h Label1: MOV AX, v1[CX] SS:FFF6h DS:0002h 08h PUSH AX ;PUSH works on 16bit SS:FFF7h DS:0003h 00h MOV DS:[SI],AX ; ES:[BX] works SS:FFF8h 06h DS:0004h 07h ; ES:[CX]doesn’t SS:FFF9h 00h DS:0005h 00h ADD SI, 2 ; next byte (SI=SI+2). SS:FFFAh 07h DS:0005h 06h CMP AX, 6 SS:FFFBh 00h DS:0005h 00h LOOPNE label1 ; SS:FFFCh 08h DS:0005h RET SS:FFFDh 00h... v1 DW 5, 6, 7, 8, 9 SS:FFFEh 09h SS:FFFFh 00h 58 H Exercise [Answers] What does bellow program do? represent the stored results. STACK Give the final value of all involved registers in hexadecimal SS:[SP] SS:0000h ORG 100h The program loops 5 times SS:0001h MOV CX, 05 that is from CX=5 to CX=1. all CX value are loaded in SS:0002h TAG PUSH CX SS:0003h the stack. LOOP TAG ; SS:0004h RET The stored values are as SS:0005h presented............ Final value of registers are: CX=0000h , SP=FFF6h SS:FFF5h SS:FFF6h 01h SS:FFF7h 00h SS:FFF8h 02h SS:FFF9h 00h SS:FFFAh 03h SS:FFFBh 00h SS:FFFCh 04h SS:FFFDh 00h SS:FFFEh 05h SS:FFFFh 00h 59 H Exercise [Answers] What does bellow program do? represent the stored results. STACK Give the final value of all involved registers in hexadecimal SS:[SP] SS:0000h ORG 100h The program loops only 3 SS:0001h MOV AX, 09 times because AX decrement starting from SS:0002h MOV CX, 08 SS:0003h 09 but stops at 07. TAG DEC AX SS:0004h PUSH AX The stored values are as SS:0005h CMP AX, 07h presented...... LOOPNE TAG ;...... RET Final value of registers are: AX=0007h , CX=0006h , SS:FFF5h SP=FFFAh SS:FFF6h SS:FFF7h SS:FFF8h SS:FFF9h SS:FFFAh 07h SS:FFFBh 00h SS:FFFCh 08h SS:FFFDh 00h SS:FFFEh 09h SS:FFFFh 00h 60 H Exercise [Answers] What does bellow program do? represent the stored results. MEMORY STACK Give the final value of all involved registers in hexadecimal DS:[SI] SS:[SP] DS:0000h 00h SS:0000h ORG 100h DS:0001h 11h SS:0001h V DW 0011h 0022h 0033h 0044h 5555h 0066h DS:0002h 00h SS:0002h MOV SI, 0 DS:0003h 22h SS:0003h MOV BX, 0 DS:0004h 00h SS:0004h MOV CX, 6 DS:0005h 33h SS:0005h TAG MOV AX, V[BX] DS:0006h 00h...... PUSH AX DS:0007h 44h...... MOV DS:[SI], AX DS:0008h 55h SS:FFF5h ADD SI, 2 DS:0009h 55h SS:FFF6h 55h INC BX DS:000Ah SS:FFF7h 55h CMP AX, 5555h DS:000Bh SS:FFF8h 44h LOOPNE TAG...... SS:FFF9h 00h RET SS:FFFAh 33h The program loops only 5 times because AX reads V[BX] but stops...... SS:FFFBh 00h at the fifth value 5555h. DS:FFFCh SS:FFFCh 22h The stored values are as presented DS:FFFDh SS:FFFDh 00h Final value of registers are: DS:FFFEh SS:FFFEh 11h AX=5555h , BX=0005h , CX=0001h , SI=000Ah , SP=FFF6h DS:FFFFh SS:FFFFh 00h 61 H Common instructions Transfer instructions Syntax: < PUSH > < POP > PUSH AX ; Transfers the contents of AX to the top of the stack POP BX ; Transfers the top of the stack to the BX register PUSHF ; Loads the PSW status register into the top of the stack POPF ; Transfers the top of the stack to the PSW register Example PUSH AX ; AX to stack POP BX ; stack to BX 62 H Common instructions Transfer instructions < PUSH > < POP > after some operations AX changed Note: POP doesn’t delete the stacked data To modify the bit TF: PUSHF // stack the PSW data in the stack memory POP AX // put the stack memory data in AX (AX=PSW) OR AX, 0000000100000000b (for TF=1) // AND AX, 1111111011111111b (for TF=0) PUSH AX // put AX in the stack memory POPF // put the stack memory in PSW (PSW=AX) 63 H Common instructions Transfer instructions Syntax: XCHG Op1, Op2 ; xchange Op1 with Op2 Allows data to be moved from one location (source) to another location (destination) Register to register Register to memory Memory to register Example AX=1000 ; BX=2000 XCHG AX, BX ; xchange AX with BX AX=2000 ; BX=1000 64 H Common instructions Transfer instructions CMP Op1, Op2 ; subtracts Op1 with Op2 This instruction takes two operands and subtracts one from the other, then sets OF, SF, ZF, AF, PF, and CF flags accordingly. The result is not stored anywhere. The operand1 operand can be a register or memory address, and operand2 can be a register, memory, or immediate value. Example CMP AX, BX ; subtracts AX with BX 65 H Common instructions Transfer instructions JMP label ; jump to label The jump instructions transfer the program execution to a new set of instructions indicated by the label provided as an operand. There are two types of jump instructions. Unconditional jump and conditional jumps Unconditional jump (JMP): It directly jumps to the provided label. Conditional jump: These instructions are used to jump only if a condition is satisfied and called after CMP instruction. This instruction first evaluates if the condition is satisfied through flags, then jumps to the label given as operand. There are 31 conditional jump instructions available in 8086 assembly language. ‘’Jcond’’ instruction : Conditional jump Syntax: Jcond Label Result : jump to ‘’Label’’ if the condition ‘’cond’’ is verified (Jump if valid), otherwise, the execution continues sequentially to the next instruction Example CMP AX, BX ; subtracts AX with BX JMP label1 ; jump to label1 MOV AX, 5 Label1: MOV AX, 8 66 H Common instructions Transfer instructions CMP Op1, Op2 ; operand1 - operand2 : Flags affected (OF, SF, ZF, AF, PF, CF) JMP ; always jump Instruction Condition Algorithm Set by CMP, SUB, ADD, JZ Label Jump if Zero if ZF = 1 then jump TEST, AND, OR, XOR JNZ Label Jump if not Zero if ZF = 0 then jump JE Label Jump if equal if ZF = 1 then jump JNE Label Jump if not equal if ZF = 0 then jump JA Label Jump if > if (CF = 0) and (ZF = 0) then jump UnSigned JAE Label Jump if >= if CF = 0 then jump Nombres JB Label Jump if < if CF = 1 then jump set by CMP JBE Label Jump if if (ZF = 0) and (SF = OF) then jump JGE Label Jump if >= if SF = OF then jump Signed JL Label Jump if < if SF OF then jump Nombres JLE Label Jump if =0, after CMP NEG AX Lp1 : RET Exercise What does the following program do? ORG 100h ; MOV AL, [1100h] ; ADD AL, [1101h] ; JS Neg JZ Nul MOV ES:[1102h], AL JMP Fin Neg: MOV ES:[1103h], AL JMP Fin ; Nul: MOV ES:[1104h], AL Fin: RET 70 H Program Structure 71 H Assembly language High/Low level languages Machine language Assembly language Instruction’s Addressing modes Common instructions Transfer instructions Arithmetic instructions Logical instructions Jump/loop instructions Program Structure Assembler Editors variables Subprogram macro Interruptions Summery Practice Common mistakes 72 H Assembly Editor TF=0: step-by-step execution TF=1: block execution 73 H Variables 74 H Program Structure Working with variables The.data directive defines the section that contains permanent constants and variables used in the program. The.code directive defines the section that contains program instructions ORG 100h.data v1 DB 9, 8, 7, 6, 5 a1 DB 'H', 'e', 'l', 'l', 'o' data DB 11h, 22h, 33h, 44h, 55h.code MOV CX, 5 P1: MOV BX, CX MOV AL, data[BX-1] ; data[AX] data[CX] data[DX] don't work MOV AH, v1[BX-1] ; MOV DL, a1[BX-1] LOOP P1 RET END 75 H Program Structure Working with variables Variable is a memory location. There are two types of variables: ; Data segment.data num DB 31H char DB 'A' output DW "Hello, everyone!! $" Input_char DB ?.code MOV, AX, 5 76 H Program Structure Working with variables Working with variables In an assembly program, all variables are declared in the data segment. The emu8086 provides some define directives for declaring variables. Specifically, we'll use DB (define byte) and DW (define word) directives which allocates 1 byte and 2 bytes respectively. variable-name is the identifier for each storage space. The assembler associates an offset value for each variable name defined in the data segment. example variable declaration of num and char, where we initialize num and char with a value that can be changed later. We can use ? to indicate that the value is currently unknown. We cannot use the variables in the code segment unless to first move the address of the data segment to the DS (data segment) register. We can use (.data) and (.code) derivatives to bring all variables to the program. 77 H Program Structure Label Label: A label is a symbolic name for the address of the instruction that is given immediately after the label declaration. It can be placed at the beginning of a statement and serve as an instruction operand. Labels are of two types. Symbolic Labels: A symbolic label consists of an identifier or symbol followed by a colon (:). They must be defined only once as they have global scope and appear in the object file's symbol table. Numeric Labels: A numeric label consists of a single digit in the range zero (0) through nine (9) followed by a colon (:). They are used only for local reference and excluded in the object file's symbol table. Hence, they have a limited scope and can be re-defined repeatedly. label1: MOV CX, 4 DEC CX JCXZ label1 ; Jump if CX register is 0 JMP 1 2: MOV AX, 7 1: MOV CX, 3 JMP 2 78 H Subprogram 79 H to use a procedure use CALL instruction, for example: CALL pro1 the procedure starts at its name and ends at the ENDP directive. procedures, can be defined above or below the main program. A Procedure is located at some specific address in memory, and if we use the same procedure many times, the CPU will transfer control to this part of the memory at each time. The control will be returned back to the program by RET instruction. The stack is used to maintain the return address. The CALL instruction takes about 3 bytes, so the size of the output executable file grows insignificantly, no matter how many time the procedure is used. The stack or any general purpose registers are used to pass parameters to procedure. Transfers control to procedure (subprogram ). Transfers control to procedure, return address ORG 100h ; directive ORG (IP) is pushed to stack. CALL pro1 (this is a near call), ADD AX, 1 RET ; return to OS. For a far call (when exceeding CS capacity), 4- pro1 PROC ; procedure declaration. bytes address is entered in MOV AX, 1234h this form: 1234h:5678h, RET ; return to caller. first value is a segment ENDP ; directive ENDP second value is an offset (this is a far call, so CS is also pushed to stack, to be able to return). 80 H macro 81 R H Program Structure macro Since some instructions repeat constantly in a program, writing macro functions (or macros) is a convenient way to make your source code more readable. It is possible to choose a name for certain sequences of instructions that represents them. When the compiler (actually, the preprocessor) encounters this name in your source code, it will replace it with the lines of code it designates. These lines form a “macro”. if a macro is called fifteen times in the program, the compiler will write the code for that macro fifteen times during compilation. This is the difference compared to a procedures which are only written once, but can be called as often as you needed using a CALL. So, unlike a procedure, a macros, as instruction, has no meaning for the assembly compiler. 82 https://yassinebridi.github.io/asm-docs/asm_tutorial_10.html H Program Structure macro Making Macro is as making new instruction but it is in fact a set of instruction Unlike procedures, macro should be defined above the code that uses it. to use a macro, type its name. For example: macro1 another example: Beep or PRINT Macro is expanded directly in program's code. So if we use the same macro 100 times, the compiler expands the macro 100 times, making the output executable file larger and larger, because at each time all instructions of a macro are reinserted. Beep MACRO MOV DL,07h MOV AH,02h INT 21h ; generate «Beep» sound when DL=07h, AH,02h ENDM ORG 100h Beep Beep Beep Beep RET 83 H Program Structure macro To pass parameters to macro, just type them after the macro name. For example: PRINT 1, 2, 3 or PRINT message To mark the end of the macro ENDM directive is enough. PRINT MACRO message MOV DX, offset message ; direct DX to msg ; msg must be below MOV AH, 9 INT 21H ; generate message on screen ENDM ; directive ENDM ORG 100h ; directive ORG MOV AL, a ADD AL, 1 JC label1 PRINT msg1 JMP exit label1: PRINT msg2 exit: RET a EQU 255 ; directive EQU ; a=255 msg1 DB "no carry $" ; directive DB msg2 DB "has carry $" ; directive DB 84 H Program Structure macro PRINT MACRO message MOV DX, offset message ; direct DX to msg ; msg must be below MOV AH, 9 INT 21H ; generate message on screen ENDM ; directive ENDM ORG 100h MOV CX, a label1: PRINT msg1 DEC CX JCXZ label1 ; Jump if CX register is 0 PRINT msg2 RET a EQU 6 ; directive EQU ; a=6 msg1 DB " CX is not zero $" ; directive DB msg2 DB " 'CX is zero $" ; directive DB 85 H Program Structure macro Macros are expanded directly in code, therefore if there are labels inside the macro definition you may get "Duplicate declaration" error when macro is used for twice or more. To avoid such problem, use LOCAL directive followed by names of variables, labels or procedure names. For example: MyMacro2 MACRO LOCAL label1, label2 CMP AX, 2 JE label1 CMP AX, 3 JE label2 label1: INC AX label2: ADD AX, 2 ENDM ORG 100h MyMacro2 MyMacro2 RET to use a macros in several programs, it may be a good idea to place all macros in a separate file. Place that file in Inc folder and use INCLUDE file-name directive to use all macros in the file. See Library of common functions - emu8086.inc for an example of such file. 86 https://yassinebridi.github.io/asm-docs/asm_tutorial_10.html H Program Structure macro Macro definition: MyMacro MACRO p1, p2, p3 MOV AX, p1 MOV BX, p2 name MACRO [parameters,...] MOV CX, p3 ENDM ORG 100h ENDM MyMacro 1, 2, 3 MyMacro 4, 5, DX Macros are just like procedures, but not really. Macros look RET like procedures, but they exist only until your code is ; the above code is expanded into: compiled, after compilation all macros are replaced with real instructions. If you declared a macro and never used it MOV AX, 00001h in your code, compiler will simply ignore it. emu8086.inc is MOV BX, 00002h a good example of how macros can be used, this file MOV CX, 00003h contains several macros to make coding easier for you. MOV AX, 00004h If you plan to use your macros in several programs, it may MOV BX, 00005h be a good idea to place all macros in a separate file. Place MOV CX, DX that file in Inc folder and use INCLUDE file-name directive to use macros. See Library of common functions - emu8086.inc for an example of such file. 87 H Interruptions 88 H Interruptions DEFINITION An interruption is generated task in response of an event, whether the event is expected (like reading from keyboard) or unexpected (like printing error). Once the event is finished and the interruption function is performed, the processor resume back to the normal running of the program. TYPES OF INTERRUPTIONS System interruptions : reset, hardware error,… etc. Exception : error while running instructions, example: invalid instruction or division per 0 Hardware interruptions : generated by peripherals, example: keyboard , printer, … etc. Software interruptions : generated by the program developer using instructions The difference between interruption and subroutine The difference between interruption and a called subprogram procedure is that the interruption interrupt the normal program running to either unexpected event or expecting an even. Those events are out of the control of the main program. The subprogram procedure however has no external event except if a software interruption is introduced. 89 H Interruptions There are so many interruption in every computer system. Below are examples of a system with 8086 microprocessor... INT 1Ah / AH = 00h - get system time.... INT 10h is used for graphic display mode INT 11h get BIOS equipment list.... INT 13h is used for mass storage (disk, floppy) access... INT 16h is used to locate the keyboard hits. INT 17h is used for printer functions... INT 21h / AH= 39h - make directory.... INT 21h / AH= 3Ah - remove directory INT 19h is used to reboot the system INT 21h / AH= 3Bh - set current directory INT 20h, exit to operating system. INT 21h / AH= 3Dh - open existing file INT 21h is used for all DOS services INT 21h / AH= 56h - rename file / move file... INT 21h / AH= 3Fh - read from file INT 33h, is used for the mouse...... 90 H Interruptions 𝜇 P8086 Interruptions For 8086 systems, each interruption represents a functions family, where usually the value stored in the AH register represents the function number. INT 10h is used for screen manipulation INT 13h is used for storage (HDD and FDD) AH=00h Set video mode AH=42h DISK READ AX=1003h Set Blinking mode AH=43h DISK WRITE AH=13h write string AH=03h Get cursor position INT 0x16 is for Keyboard control and read AH=00h GetKey INT 21h is used for DOS services AH=03h Set typematic rate and delay AH=00h Set video mode AX=1003h Set Blinking mode AH=13h write string AH=03h Get cursor position these are just BIOS INT, which can be rewritten by OS during startup. 91 H Interruptions INT 10H INT 10h AH = 00 ; set video mode. AL = desired video mode. AL = 00h - text mode. 40x25. 16 colors. 8 pages. AL = 03h - text mode. 80x25. 16 colors. 8 pages. AL = 13h - graphical mode. 40x25. 256 colors. 320x200 pixels. 1 page. example: MOV AL, 13h MOV AH, 0 INT 10h INT 10h AX = 1003h ; toggle intensity/blinking. BL = write mode BL = 0: enable intensive colors. BL = 1: enable blinking (not supported by the emulator). BH = 0 (to avoid problems on some adapters). example: MOV AX, 1003h MOV BX, 0 INT 10h 92 H INT 0x10 with AH = 0x00 and AL = 3 (mov ax, 3) means: set video mode to TextMode 80x25 chars and 16 colors. INT 0x10 with AX = 0x1003 means: TOGGLE INTENSITY/BLINKING BIT to background intensity enabled name "hi-world“ ; useless declaration name ORG 100h MOV AX, 0003h ; AL = 3 : set video mode to TextMode 80x25 chars and 16 colors. INT 10h MOV AX, 1003h ; AX = 0x1003 : INTENSITY/BLINKING BIT to background intensity enabled MOV BX, 0 INT 10h MOV AX, 0b800h MOV DS, AX ; print characters on the screen one by one up to 80 ; at the same time character stored in DS:XX MOV [02h], 'H‘ MOV [04h], 'e‘ MOV [06h], 'l‘ MOV [08h], 'l‘ MOV [0Ah], 'o‘ MOV [0Ch], '!‘ MOV CX, 06 ; loop number of characters (6). MOV DI, 03h ; start from byte 'h‘ T: MOV [DI], 11101100b ; 1110-1100b yellow color - background by character ADD DI, 2 ; skip over next ascii code in vga memory. LOOP T ; wait for any key press to continue and return (AL=key-pressed ASCII) MOV AH, 01 INT 21h RET 93 H Interruptions INT 21H INT 21h AH=01 ; read character from standard input, with echo, - result is stored in AL. if there is no character in the keyboard buffer, the function waits until any key is pressed. example: MOV AH, 01 INT 21h INT 21h AH=4Ch ; return to dos , The INT 21h+function 4ch is introduced to put an end for an.EXE program file.asm to.exe. example: MOV AH, 4ch INT 21h T: MOV