Br. Athaur Rahman Bin Najeeb Room 2.105 Email: athaur@iium.edu.my Website: http://eng.iiu.edu.my/~athaur Consultation : Tuesday 10.00 am ( appointment) Announcement Chapter 5:P 8088/8086 Programming Recall of debugging Assembler Instructions Data transfer instructions Arithmetic instructions Logic instructions Logic instructions Shift instructions Rotate instructions Useful Link: http://oopweb.com/Assembly/Documents/Art OfAssembly/Volume/Chapter_6/CH06-1.html Debug Debug is a little tool that comes with windows Part of PCs Disk Operating System ( DOS) Invoking debug Start -> Run-> type cmd -> typedebug Start -> Run ->type debug Debug Commands / Monitor Commands A I T U U G H M E F R C Structure of ASM file Mnemonic Meaning Format Operation Flags affected Mov Move Mov D,S (s) (D) None Data Transfer Instructions - MOV Destination Source Memory Accumulator Accumulator Memory Accumulator Memory Register Register Register Memory Memory Register Register Immediate Memory Immediate Seg reg Reg 16 Seg reg Mem 16 Reg 16 Seg reg Memory Seg reg NO MOV Mem Imm Seg Reg Mem Seg Reg Seg reg EX: MOV AL, BL Data Transfer Instruction -MOV The MOV instruction Move data from source to destination; e.g. MOV [DI+100H], AH It does not modify flags Cannot transfer data between 2 external memory address Eg: MOV [SI],[1234] Cannot move (load) directly to Segment Registers Eg: MOV DS, 1000 Byte Operation / Word Operation Question [Example 5.1 , text book ? ] What is the effect of executing MOV CX, [ SOURCE_MEMORY ] .. Where SOURCE_MEMORY equal 20(H) and DS equal 1A00[H] SOURCE_MEMORY equal 20(H) and DS equal 1A00[H] Ex: Mov dx,cs Data Transfer Instructions - XCHG Mnemonic Meaning Format Operation Flags affected XCHG Exchange XCHG D,S (s) (D) None Destination Source Accumulator Reg 16 Memory Register Register Register Register Memory Example: XCHG [1234h], BX NO XCHG MEMs SEG REGs Example XCHG Example: For the data shown, what is the result of executing XCHG [SUM], Bx ? [SUM] = 1234(16) Solution: Execution of this instruction performs the operation ((DS)0 + SUM) (BX) The PA is PA = 12000(16) + 1234(16) = 13234(16) PA = 12000(16) + 1234(16) = 13234(16) Then, 13234(16) (BL) 13235(16) (BH) Result: (BX) = 00FF(16) Your First Program Write a ASM program to load values 0011(H), 00AB(H) into regiter AX, BX and swap them Read from memory location 0200(H) and swap with register AX Step 1: Flow Chart (? ) Step 2 Translate to Assembly Language Step 3: write, test, debug and execute the code Step 3: write, test, debug and execute the code Answer Data Transfer Instructions LEA, LDS, LES Loading GP registers and segment registers with an address directly from memory Ex: LEA SI,[EA] LEA SI, [DI+BX+5H] ; DI=1000(H), Bx=20(H), EA ? PA = DS[0] +EA LEA SI, [200h] , if DS=1200 , calculate the PA LEA Destination, Source Transfers the offset address of source (must be a memory location) to the destination register It does not modify flags LDS Destination Source LES Destination Source It is identical to LDS except that the second two bytes are copied to ES It does not modify flags LDS Destination Source Load 4-byte data (pointer) in memory to two 16-bit registers Source operand gives the memory location The first two bytes are copied to the register specified in the destination operand; the second two bytes are copied to register DS It does not modify flags Data Transfer Instructions LEA, LDS, LES Mnemoni c Meaning Format Operation Flags affected LEA Load Effective Address LEA Reg16,EA EA (Reg16) None LDS Load Register And DS LDS Reg16,MEM32 (MEM32) (Reg16) None (Mem32+2) (DS) LES Load Register and ES LES Reg16,MEM32 (MEM32) (Reg16) (Mem32+2) (DS) None LEA SI DATA or MOV SI Offset DATA Examples for LEA, LDS, LES DATAX DW 1000H DATAY DW 5000H .CODE LEA SI, DATAX MOVE DI, OFFSET DATAY; THIS IS MORE EFFICIENT LEA BX,[DI]; IS THE SAME AS MOV BX,DI; THIS JUST TAKES LESS CYCLES. LEA BX,DI; INVALID! LES similar to LDS except that it loads ES LDS BX, [DI]; 127A BX 1000 DI 3000 DS 7A 12 00 30 11000 11001 11002 11003 Data Transfer Instructions SAHF LAHF Store data in AH to the low 8 bits of the flag register It modifies flags: AF, CF, PF, SF, ZF Copies bits 0-7 of the flags register into AH It does not modify flags LDS Destination Source Load 4-byte data (pointer) in memory to two 16-bit registers Source operand gives the memory location The first two bytes are copied to the register specified in the destination operand; the second two bytes are copied to register DS It does not modify flags LES Destination Source It is identical to LDS except that the second two bytes are copied to ES It does not modify flags Arithmetric Instruction Addition Subtraction Multiplication Division Arithmetic Instructions ADD Destination, Source Destination + Source Destination Destination and Source operands can not be memory locations at the same time It modifies flags AF CF OF PF SF ZF ADC Destination, Source Destination + Source + Carry Flag Destination Destination + Source + Carry Flag Destination Destination and Source operands can not be memory locations at the same time It modifies flags AF CF OF PF SF ZF INC Destination Destination + 1 Destination It modifies flags AF OF PF SF ZF (Note CF will not be changed) DEC Destination Destination - 1 Destination It modifies flags AF OF PF SF ZF (Note CF will not be changed) Arithmetic Instructions ADD, ADC, INC, AAA, DAA Mnemonic Meaning Format Operation Flags affected ADD Addition ADD D,S (S)+(D) (D) carry (CF) ALL ADC Add with carry ADC D,S (S)+(D)+(CF) (D) carry (CF) ALL carry carry (CF) INC Increment by one INC D (D)+1 (D) ALL but CY AAA ASCII adjust for addition AAA If the sum is >9, AH is incremented by 1 AF,CF DAA Decimal adjust for addition DAA Adjust AL for decimal Packed BCD ALL Arithmetic Instructions SUB, SBB, DEC, AAS, DAS, NEG Mnemonic Meaning Format Operation Flags affected SUB Subtract SUB D,S (D)-(S) (D) Borrow (CF) All SBB Subtract with borrow SBB D,S (D)-(S)-(CF) (D) All DEC Decrement by one DEC D (D)-1 (D) All but CF NEG Negate NEG D All DAS Decimal adjust for subtraction DAS Convert the result in AL to packed decimal format All AAS ASCII adjust for subtraction AAS (AL) difference (AH) dec by 1 if borrow CY,AC Arithmetic Instructions SUB Destination, Source Destination - Source Destination Destination and Source operands can not be memory locations at the same time It modifies flags AF CF OF PF SF ZF SBB Destination, Source Destination - Source - Carry Flag Destination Destination - Source - Carry Flag Destination Destination and Source operands can not be memory locations at the same time It modifies flags AF CF OF PF SF ZF CMP Destination, Source Destination Source (the result is not stored anywhere) Destination and Source operands can not be memory locations at the same time It modifies flags AF CF OF PF SF ZF (if ZF is set, destination = source) Example 1 Write a program to add a data byte located at offset 0500(h) in 2000h data segment WITH an other byte which is available in 0700h in the same segment Example 2 Write a program to move the contects of the ML 0500h to register BX and to register CX. Add immediate byte 05h to the data residing in ML whose address computed using ds-2000h and offset 0600h. Store the result of addition in 0700h. Assume all data is located in ds=2000h start Move content to BX Move content to BX Move content to CX Move data , 05h to [0600h] Store result End Arithmetic Instructions MUL Source Perform unsigned multiply operation If source operand is a byte, AX = AL * Source If source operand is a word, (DX AX) = AX * Source Source operands can not be an immediate data It modifies CF and OF (AF,PF,SF,ZF undefined) IMUL Source IMUL Source Perform signed binary multiply operation If source operand is a byte, AX = AL * Source If source operand is a word, (DX AX) = AX * Source Source operands can not be an immediate data It modifies CF and OF (AF,PF,SF,ZF undefined) Examples: MOV AL, 20H MOV CL, 80H MUL CL MOV AL, 20H MOV CL, 80H IMUL CL DIV Source Perform unsigned division operation If source operand is a byte, AL = AX / Source; AH = Remainder of AX / Source If source operand is a word, AX=(DX AX)/Source; DX=Remainder of (DX AX)/Source Source operands can not be an immediate data IDIV Source Arithmetic Instructions Examples: MOV AX, 5 MOV BL, 2 DIV BL MOV AL, -5 MOV BL, 2 IDIV BL Perform signed division operation If source operand is a byte, AL = AX / Source; AH = Remainder of AX / Source If source operand is a word, AX=(DX AX)/Source; DX=Remainder of (DX AX)/Source Source operands can not be an immediate data Summary so far Arithmetic Instructions NEG Destination 0 Destination Destination (the result is represented in 2s complement) Destination can be a register or a memory location It modifies flags AF CF OF PF SF ZF CBW Extends a signed 8-bit number in AL to a signed 16-bit data and stores it into AX It does not modify flags It does not modify flags CWD Extends a signed 16-bit number in AX to a signed 32-bit data and stores it into DX and AX. DX contains the most significant word It does not modify flags Other arithmetic instructions: DAA, DAS, AAA, AAS, AAM, AAD Example What is the result of executing the following sequence of instruction ? MOV AL, 0A1H CBW CWD Logical Instructions ( 3) NOT Destination Inverts each bit of the destination operand Destination can be a register or a memory location It does not modify flags AND Destination, Source Performs logic AND operation for each bit of the destination and source; stores the result into destination result into destination Destination and source can not be both memory locations at the same time It modifies flags: CF OF PF SF ZF OR Destination, Source Performs logic OR operation for each bit of the destination and source; stores the result into destination Destination and source can not be both memory locations at the same time It modifies flags: CF OF PF SF ZF Logic operation: a review Example: Describe the result of executing the following sequence of instructions: MOV AL,01010101B AND AL,00011111B AND AL,00011111B OR AL,11000000B XOR AL,00001111B NOT AL Clearing Setting, Toggling bit of an Operand Mask : clear a bit to zero To clear a bit, we AND it with zero AND with 1 : unchanged Example: AND AX, 000F; if AX = FFFF Setting a bit to 1: OR with logic 1 XOR to reverse the login level AND AL, 0FH (given AL=FF) Application example: A microprocessor is connected to read 8 LEDs into AL , if LED number 0 is on, then turn on the alarm SHIFT INSTRUCTIONS A logical shift fills the newly created bit position with zero: Logical VS Arithmetic Shifts CF 0 An arithmetic shift fills the newly created bit position with a copy of the numbers sign bit: CF SHIFT ARITHMETIC LOGICAL Shift Instructions : 4 types LEFT LEFT RIGHT RIGHT SAL SAR SHL SHR Bit Manipulation Instructions SHL(SAL) Destination, Count Left shift destination bits; the number of bits shifted is given by operand Count During the shift operation, the MSB of the destination is shifted into CF and zero is shifted into the LSB of the destination Operand Count can be either an immediate data or register CL Destination can be a register or a memory location It modifies flags: CF OF PF SF ZF CF 0 Destination CF 0 SHR Destination, Count Right shift destination bits; the number of bits shifted is given by operand Count During the shift operation, the LSB of the destination is shifted into CF and zero is shifted into the MSB of the destination Operand Count can be either an immediate data or register CL Destination can be a register or a memory location It modifies flags: CF OF PF SF ZF CF 0 Destination Destination LSB MSB LSB MSB Bit Manipulation Instructions SAR Destination, Count Right shift destination bits; the number of bits shifted is given by operand Count The LSB of the destination is shifted into CF and the MSB of the destination remians the same Operand Count can be either an immediate data or register CL Destination can be a register or a memory location It modifies flags: CF PF SF ZF CF Destination LSB MSB Fast Multipication ROTATE INSTRUCTIONS Bit Manipulation Instructions ROL Destination, Count Left shift destination bits; the number of bits shifted is given by operand Count The MSB of the destination is shifted into CF, it also goes to the LSB of the destination Operand Count can be either an immediate data or register CL Destination can be a register or a memory location It modifies flags: CF OF CF Destination LSB MSB ROR Destination, Count Right shift destination bits; the number of bits shifted is given by operand Count The LSB of the destination is shifted into CF, it also goes to the MSB of the destination Operand Count can be either an immediate data or register CL Destination can be a register or a memory location It modifies flags: CF OF CF Destination LSB MSB Bit Manipulation Instructions RCL Destination, Count Left shift destination bits; the number of bits shifted is given by operand Count The MSB of the destination is shifted into CF; the old CF value goes to the LSB of the destination Operand Count can be either an immediate data or register CL Destination can be a register or a memory location It modifies flags: CF OF PF SF ZF CF Destination LSB MSB RCR Destination, Count Right shift destination bits; the number of bits shifted is given by operand Count The LSB of the destination is shifted into CF, the old CF value goes to the MSB of the destination Operand Count can be either an immediate data or register CL Destination can be a register or a memory location It modifies flags: CF OF PF SF ZF CF Destination LSB MSB Example: Using ROL to Exchange Values. .data byteval db 0Fh wordval dw 1234h .code byte_values: MOV AL,26h MOV AL,26h ROL AL,4 ; AL = 62h ROL byteval,4 ; byteval = F0h word_values: MOV AX,0203h ROL AX,8 ; AX = 0302h ROL wordval,8 ; wordval = 3412h Example: What is the result in BX and CF after execution of the following instruction? RCR BX,CL Given (CL) = 04H, (BX)= 1234H and (CF) = 0 Given (CL) = 04H, (BX)= 1234H and (CF) = 0 Example: Indicate the hexadecimal value of AL after each rotation MOV AL,6BH ROR AL,1 RCL AL,3 ANSWER: A B5H A B5H B AEH