Selected Pentium Instructions. Chapter 12 S. Dandamudi

Size: px
Start display at page:

Download "Selected Pentium Instructions. Chapter 12 S. Dandamudi"

Transcription

1 Selected Pentium Instructions Chapter 12 S. Dandamudi

2 Outline Status flags Zero flag Carry flag Overflow flag Sign flag Auxiliary flag Parity flag Arithmetic instructions Multiplication instructions Division instructions Application examples PutInt8 GetInt8 Conditional execution Indirect jumps Conditional jumps Single flags Unsigned comparisons Signed comparisons Implementing high-level language decision structures Selection structures Iteration structures S. Dandamudi Chapter 12: Page 2 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

3 Outline (cont d) Logical expressions in high-level languages Representation of Boolean data Logical expressions Logical expression evaluation Full evaluation Partial evaluation Bit instructions Bit test and modify Bit scan Illustrative examples String representation String instructions Repetition prefixes Direction flag String move instructions String compare instructions String scan instructions Illustrative examples str_len str_mov Indirect procedure call S. Dandamudi Chapter 12: Page 3 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

4 Status Flags S. Dandamudi Chapter 12: Page 4 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

5 Status Flags (cont d) Status flags are updated to indicate certain properties of the result Example: If the result is zero, zero flag is set Once a flag is set, it remains in that state until another instruction that affects the flags is executed Not all instructions affect all status flags add and sub affect all six flags inc and dec affect all but the carry flag mov, push, and pop do not affect any flags S. Dandamudi Chapter 12: Page 5 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

6 Status Flags (cont d) Example ; initially, assume ZF = 0 mov AL,55H ; ZF is still zero sub AL,55H ; result is 0 ; ZF is set (ZF = 1) push BX ; ZF remains 1 mov BX,AX ; ZF remains 1 pop DX ; ZF remains 1 mov CX,0 ; ZF remains 1 inc CX ; result is 1 ; ZF is cleared (ZF = 0) S. Dandamudi Chapter 12: Page 6 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

7 Status Flags (cont d) Zero Flag Indicates zero result If the result is zero, ZF = 1 Otherwise, ZF = 0 Zero can result in several ways (e.g. overflow) mov AL,0FH mov AX,0FFFFH mov AX,1 add AL,0F1H inc AX dec AX» All three examples result in zero result and set ZF Related instructions jz jump if zero (jump if ZF = 1) jnz jump if not zero (jump if ZF = 0) S. Dandamudi Chapter 12: Page 7 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

8 Uses of zero flag Status Flags (cont d) Two main uses of zero flag» Testing equality Often used with cmp instruction cmp char, $ ; ZF = 1 if char is $ cmp AX,BX» Counting to a preset value Initialize a register with the count value Decrement it using dec instruction Use jz/jnz to transfer control S. Dandamudi Chapter 12: Page 8 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

9 Status Flags (cont d) Consider the following code sum := 0 for (i = 1 to M) for (j = 1 to N) sum := sum + 1 end for end for Assembly code sub AX,AX ; AX := 0 mov DX,M outer_loop: mov CX,N inner_loop: inc AX loop inner_loop dec DX jnz outer_loop exit_loops: mov sum,ax S. Dandamudi Chapter 12: Page 9 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

10 Status Flags (cont d) Two observations loop instruction is equivalent to dec DX jnz outer_loop» This two instruction sequence is more efficient than the loop instruction (takes less time to execute)» loop instruction does not affect any flags! This two instruction sequence is better than initializing DX = 1 and executing inc DX cmp DX,M jle inner_loop S. Dandamudi Chapter 12: Page 10 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

11 Status Flags (cont d) Carry Flag Records the fact that the result of an arithmetic operation on unsigned numbers is out of range The carry flag is set in the following examples mov AL,0FH mov AX,12AEH add AL,0F1H sub AX,12AFH Range of 8-, 16-, and 32-bit unsigned numbers size range 8 bits 0 to 255 (2 8 1) 16 bits 0 to 65,535 (2 16 1) 32 bits 0 to 4,294,967,295 (2 32 1) S. Dandamudi Chapter 12: Page 11 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

12 Status Flags (cont d) Carry flag is not set by inc and dec instructions» The carry flag is not set in the following examples mov AL,0FFH mov AX,0 inc AL dec AX Related instructions jc jump if carry (jump if CF = 1) jnc jump if no carry (jump if CF = 0) Carry flag can be manipulated directly using stc set carry flag (set CF to 1) clc clear carry flag (clears CF to 0) cmc complement carry flag (inverts CF value) S. Dandamudi Chapter 12: Page 12 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

13 Status Flags (cont d) Uses of carry flag To propagate carry/borrow in multiword addition/subtraction 1 carry from lower 32 bits x = A AE7H y = 489B A321 FE H 7FAB C9CA 10B7 DCFAH To detect overflow/underflow condition» In the last example, carry out of leftmost bit indicates overflow To test a bit using the shift/rotate instructions» Bit shifted/rotated out is captured in the carry flag» We can use jc/jnc to test whether this bit is 1 or 0 S. Dandamudi Chapter 12: Page 13 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

14 Status Flags (cont d) Overflow flag Indicates out-of-range result on signed numbers Signed number counterpart of the carry flag The following code sets the overflow flag but not the carry flag mov AL,72H ; 72H = 114D add AL,0EH ; 0EH = 14D Range of 8-, 16-, and 32-bit signed numbers size range 8 bits 128 to to (2 7 1) 16 bits 32,768 to +32, to (2 15 1) 32 bits 2,147,483,648 to +2,147,483, to (2 31 1) S. Dandamudi Chapter 12: Page 14 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

15 Status Flags (cont d) Signed or unsigned: How does the system know? The processor does not know the interpretation It sets carry and overflow under each interpretation Unsigned interpretation mov AL,72H add AL,0EH jc overflow no_overflow: (no overflow code here).... overflow: (overflow code here).... Signed interpretation mov AL,72H add AL,0EH jo overflow no_overflow: (no overflow code here).... overflow: (overflow code here).... S. Dandamudi Chapter 12: Page 15 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

16 Status Flags (cont d) Related instructions jo jump if overflow (jump if OF = 1) jno jump if no overflow (jump if OF = 0) There is a special software interrupt instruction into interrupt on overflow Details on this instruction in Chapter 20 Uses of overflow flag Main use» To detect out-of-range result on signed numbers S. Dandamudi Chapter 12: Page 16 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

17 Status Flags (cont d) Sign flag Indicates the sign of the result Useful only when dealing with signed numbers Simply a copy of the most significant bit of the result Examples mov AL,15 add AL,97 clears the sign flag as the result is 112 (or in binary) Related instructions js jump if sign (jump if SF = 1) jns jump if no sign (jump if SF = 0) mov AL,15 sub AL,97 sets the sign flag as the result is 82 (or in binary) S. Dandamudi Chapter 12: Page 17 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

18 Usage of sign flag Status Flags (cont d) To test the sign of the result Also useful to efficiently implement countdown loops Consider the count down loop: for (i = M downto 0) <loop body> end for If we don t use the jns, we need cmp as shown below: cmp CX,0 jl for_loop The count down loop can be implemented as mov CX,M for_loop: <loop body> dec CX jns for_loop S. Dandamudi Chapter 12: Page 18 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

19 Auxiliary flag Status Flags (cont d) Indicates whether an operation produced a carry or borrow in the low-order 4 bits (nibble) of 8-, 16-, or 32- bit operands (i.e. operand size doesn t matter) Example mov AL,43 add AL,94 1 carry from lower 4 bits 43D = B 94D = B 137D = B» As there is a carry from the lower nibble, auxiliary flag is set S. Dandamudi Chapter 12: Page 19 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

20 Status Flags (cont d) Related instructions» No conditional jump instructions with this flag» Arithmetic operations on BCD numbers use this flag aaa ASCII adjust for addition aas ASCII adjust for subtraction aam ASCII adjust for multiplication aad ASCII adjust for division daa Decimal adjust for addition das Decimal adjust for subtraction Appendices I has more details on these instructions Usage» Main use is in performing arithmetic operations on BCD numbers S. Dandamudi Chapter 12: Page 20 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

21 Status Flags (cont d) Parity flag Indicates even parity of the low 8 bits of the result PF is set if the lower 8 bits contain even number 1 bits For 16- and 32-bit values, only the least significant 8 bits are considered for computing parity value Example mov AL,53 53D = B add AL,89 89D = B 142D = B» As the result has even number of 1 bits, parity flag is set Related instructions jp jump on even parity (jump if PF = 1) jnp jump on odd parity (jump if PF = 0) S. Dandamudi Chapter 12: Page 21 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

22 Status Flags (cont d) Usage of parity flag» Useful in writing data encoding programs» Example: Encodes the byte in AL (MSB is the parity bit) parity_encode PROC shl AL jp parity_zero stc ; CF = 1 jmp move_parity_bit parity_zero: clc ; CF = 0 move_parity_bit: rcr AL parity_encode ENDP S. Dandamudi Chapter 12: Page 22 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

23 Arithmetic Instructions Pentium provides several arithmetic instructions that operate on 8-, 16- and 32-bit operands» Addition: add, adc, inc» Subtraction: sub, sbb, dec, neg, cmp» Multiplication: mul, imul Discussed in Chapter 9» Division: div, idiv» Related instructions: cbw, cwd, cdq, cwde, movsx, movzx There are few other instructions such as aaa, aas, etc. that operate on decimal numbers» See Appendix I for details S. Dandamudi Chapter 12: Page 23 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

24 Arithmetic Instructions (cont d) Multiplication More complicated than add/sub» Produces double-length results E.g. Multiplying two 8 bit numbers produces a 16-bit result» Cannot use a single multiply instruction for signed and unsigned numbers add and sub instructions work both on signed and unsigned numbers For multiplication, we need separate instructions mul for unsigned numbers imul for signed numbers S. Dandamudi Chapter 12: Page 24 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

25 Arithmetic Instructions (cont d) Unsigned multiplication mul source» Depending on the source operand size, the location of the other source operand and destination are selected S. Dandamudi Chapter 12: Page 25 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

26 Arithmetic Instructions (cont d) Example mov AL,10 mov DL,25 mul DL produces 250D in AX register (result fits in AL) Theimul instruction can use the same syntax» Also supports other formats Example mov DL,0FFH ; DL = -1 mov AL,0BEH ; AL = -66 imul DL produces 66D in AX register (again, result fits in AL) S. Dandamudi Chapter 12: Page 26 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

27 Arithmetic Instructions (cont d) Division instruction Even more complicated than multiplication» Produces two results Quotient Remainder» In multiplication, using a double-length register, there will not be any overflow In division, divide overflow is possible Pentium provides a special software interrupt when a divide overflow occurs Two instructions as in multiplication div source for unsigned numbers idiv source for signed numbers S. Dandamudi Chapter 12: Page 27 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

28 Arithmetic Instructions (cont d) Dividend is twice the size of the divisor Dividend is assumed to be in AX (8-bit divisor) DX:AX (16-bit divisor) EDX:EAX (32-bit divisor) S. Dandamudi Chapter 12: Page 28 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

29 Arithmetic Instructions (cont d) S. Dandamudi Chapter 12: Page 29 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

30 Arithmetic Instructions (cont d) Example mov AX,251 mov CL,12 div CL produces 20D in AL and 11D as remainder in AH Example sub DX,DX ; clear DX mov AX,141BH ; AX = 5147D mov CX,012CH ; CX = 300D div CX produces 17D in AX and 47D as remainder in DX S. Dandamudi Chapter 12: Page 30 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

31 Arithmetic Instructions (cont d) Signed division requires some help» We extended an unsigned 16 bit number to 32 bits by placing zeros in the upper 16 bits» This will not work for signed numbers To extend signed numbers, you have to copy the sign bit into those upper bit positions Pentium provides three instructions in aiding sign extension» All three take no operands cbw converts byte to word (extends AL into AH) cwd converts word to doubleword (extends AX into DX) cdq converts doubleword to quadword (extends EAX into EDX) S. Dandamudi Chapter 12: Page 31 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

32 Arithmetic Instructions (cont d) Some additional related instructions» Sign extension cwde converts word to doubleword (extends AX into EAX)» Two move instructions movsx dest,src (move sign-extended src to dest) movzx dest,src (move zero-extended src to dest)» For both move instructions, dest has to be a register» The src operand can be in a register or memory If src is 8-bits, dest must be either a 16- or 32-bit register Ifsrc is 16-bits, dest must be a 32-bit register S. Dandamudi Chapter 12: Page 32 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

33 Arithmetic Instructions (cont d) Example mov AL,-95 cbw ; AH = FFH mov CL,12 idiv CL produces 7D in AL and 11D as remainder in AH Example mov AX,-5147 cwd ; DX := FFFFH mov CX,300 idiv CX produces 17D in AX and 47D as remainder in DX S. Dandamudi Chapter 12: Page 33 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

34 Arithmetic Instructions (cont d) Use of Shifts for Multiplication and Division Shifts are more efficient Example: Multiply AX by 32 mov CX,32 imul CX takes 12 clock cycles Using sal AX,5 takes just one clock cycle S. Dandamudi Chapter 12: Page 34 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

35 PutInt8 procedure Application Examples To display a number, repeatedly divide it by 10 and display the remainders obtained quotient remainder 108/ / / To display digits, they must be converted to their character form» This means simply adding the ASCII code for zero line 24: add AH, 0 S. Dandamudi Chapter 12: Page 35 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

36 Application Examples (cont d) GetInt8 procedure To read a number, read each digit character» Convert to its numeric equivalent» Multiply the running total by 10 and add this digit Input digit Numeric Number := Number*10 + N value (N) Initial value * = * = * = 158 S. Dandamudi Chapter 12: Page 36 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

37 Direct jump Indirect Jumps Target address is encoded in the instruction itself Indirect jump Introduces a level of indirection» Address is specified either through memory of a generalpurpose register Example jmp CX jumps to the address in CX Address is absolute» Not relative as in direct jumps S. Dandamudi Chapter 12: Page 37 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

38 Indirect Jumps (cont d) Switch (ch) { Case 0 : count[0]++; break; Case 1 : count[1]++; break; Case 2 : count[2]++; break; Case 3 : count[3]++; break; Default: count[3]++; } S. Dandamudi Chapter 12: Page 38 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

39 Indirect Jumps (cont d) Turbo C assembly code for the switch statement _main PROC NEAR... mov AL,ch cbw sub AX,48 ; 48 = ASCII for 0 mov BX,AX cmp BX,3 Indirect ja default jump shl BX,1 ; BX = BX * 2 jmp WORD PTR CS:jump_table[BX] S. Dandamudi Chapter 12: Page 39 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

40 Indirect Jumps (cont d) case_0: inc WORD PTR [BP-10] jmp SHORT end_switch case_1: inc WORD PTR [BP-8] jmp SHORT end_switch case_2: inc WORD PTR [BP-6] jmp SHORT end_switch case_3: inc WORD PTR [BP-4] jmp SHORT end_switch default: inc WORD PTR [BP-2] end_switch:... _main ENDP S. Dandamudi Chapter 12: Page 40 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

41 Indirect Jumps (cont d) jump_table LABEL WORD DW case_0 DW case_1 DW case_2 DW case_3... Jump table for the indirect jump Indirect jump uses this table to jump to the appropriate case routine The indirect jump instruction uses segment override prefix to refer to the jump_table in the CODE segment S. Dandamudi Chapter 12: Page 41 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

42 Conditional Jumps Three types of conditional jumps Jumps based on the value of a single flag» Arithmetic flags such as zero, carry can be tested using these instructions Jumps based on unsigned comparisons» Operands of cmp instruction are treated as unsigned numbers Jumps based on signed comparisons» Operands of cmp instruction are treated as signed numbers S. Dandamudi Chapter 12: Page 42 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

43 Jumps Based on Single Flags Testing for zero jz jump if zero jumps if ZF = 1 je jump if equal jumps if ZF = 1 jnz jump if not zero jumps if ZF = 0 jne jump if not equal jumps if ZF = 0 jcxz jump if CX = 0 jumps if CX = 0 (Flags are not tested) S. Dandamudi Chapter 12: Page 43 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

44 Jumps Based on Single Flags (cont d) Testing for carry jc jump if carry jumps if CF = 1 jnc jump if no carry jumps if CF = 0 Testing for overflow jo jump if overflow jumps if OF = 1 jno jump if no overflow jumps if OF = 0 Testing for sign js jump if negative jumps if SF = 1 jns jump if not negative jumps if SF = 0 S. Dandamudi Chapter 12: Page 44 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

45 Jumps Based on Single Flags (cont d) Testing for parity jp jump if parity jumps if PF = 1 jpe jump if parity jumps if PF = 1 is even jnp jump if not parity jumps if PF = 0 jpo jump if parity jumps if PF = 0 is odd S. Dandamudi Chapter 12: Page 45 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

46 Jumps Based on Unsigned Comparisons Mnemonic Meaning Condition je jump if equal ZF = 1 jz jump if zero ZF = 1 jne jump if not equal ZF = 0 jnz jump if not zero ZF = 0 ja jump if above CF = ZF = 0 jnbe jump if not below CF = ZF = 0 or equal S. Dandamudi Chapter 12: Page 46 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

47 Jumps Based on Unsigned Comparisons Mnemonic Meaning Condition jae jump if above CF = 0 or equal jnb jump if not below CF = 0 jb jump if below CF = 1 jnae jump if not above CF = 1 or equal jbe jump if below CF=1 or ZF=1 or equal jna jump if not above CF=1 or ZF=1 S. Dandamudi Chapter 12: Page 47 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

48 Jumps Based on Signed Comparisons Mnemonic Meaning Condition je jump if equal ZF = 1 jz jump if zero ZF = 1 jne jump if not equal ZF = 0 jnz jump if not zero ZF = 0 jg jump if greater ZF=0 & SF=OF jnle jump if not less ZF=0 & SF=OF or equal S. Dandamudi Chapter 12: Page 48 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

49 Jumps Based on Signed Comparisons (cont d) Mnemonic Meaning Condition jge jump if greater SF = OF or equal jnl jump if not less SF = OF jl jump if less SF OF jnge jump if not greater SF OF or equal jle jump if less ZF=1 or SF OF or equal jng jump if not greater ZF=1 or SF OF S. Dandamudi Chapter 12: Page 49 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

50 Implementing HLL Decision Structures High-level language decision structures can be implemented in a straightforward way See Section 12.4 for examples that implement if-then-else if-then-else with a relational operator if-then-else with logical operator AND if-then-else with logical operator OR while loop repeat-until loop for loops S. Dandamudi Chapter 12: Page 50 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

51 Logical Expressions in HLLs Representation of Boolean data Only a single bit is needed to represent Boolean data Usually a single byte is used» For example, in C All zero bits represents false A non-zero value represents true Logical expressions Logical instructions AND, OR, etc. are used Bit manipulation Logical, shift, and rotate instructions are used S. Dandamudi Chapter 12: Page 51 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

52 Evaluation of Logical Expressions Two basic ways Full evaluation» Entire expression is evaluated before assigning a value» PASCAL uses full evaluation Partial evaluation» Assigns as soon as the final outcome is known without blindly evaluating the entire logical expression»two rules help: cond1 AND cond2 If cond1 is false, no need to evaluate cond2 cond1 OR cond2 If cond1 is true, no need to evaluate cond2 S. Dandamudi Chapter 12: Page 52 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

53 Evaluation of Logical Expressions (cont d) Partial evaluation Used by C Useful in certain cases to avoid run-time errors Example if ((X > 0) AND (Y/X > 100)) If x is 0, full evaluation results in divide error Partial evaluation will not evaluate (Y/X > 100) if X = 0 Partial evaluation is used to test if a pointer value is NULL before accessing the data it points to S. Dandamudi Chapter 12: Page 53 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

54 Bit Instructions Bit Test and Modify Instructions Four bit test instructions Each takes the position of the bit to be tested Instruction Effect on the selected bit bt (Bit Test) No effect bts (Bit Test and Set) selected bit 1 btr (Bit Test and Reset) selected bit 0 btc selected bit NOT(selected bit) (Bit Test and Complement) S. Dandamudi Chapter 12: Page 54 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

55 Bit Instructions (cont d) All four instructions have the same format We use bt to illustrate the format bt operand,bit_pos operand is word or doubleword» Can be in a register or memory bit_pos indicates the position of the bit to be tested» Can be an immediate value or in a 16/32-bit register Instructions in this group affect only the carry flag» Other five flags are undefined S. Dandamudi Chapter 12: Page 55 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

56 Bit Scan Instructions These instructions scan the operand for a 1 bit return the bit position in a register Two instructions bsf dest_reg,operand ;bit scan forward bsr dest_reg,operand ;bit scan reverse» operand can be a word or doubleword in a register or memory» dest_reg receives the bit position Must be a 16- or 32-bit register Only ZF is updated (other five flags undefined) ZF = 1 if all bits of operand are 0 ZF = 0 otherwise (position of first 1 bit in dest_reg) S. Dandamudi Chapter 12: Page 56 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

57 Example 1 Illustrative Examples Linear search of an integer array Example 2 Selection sort on an integer array Example 3 Multiplication using shift and add operations» Multiplies two unsigned 8-bit numbers Uses a loop that iterates 8 times Example 4 Multiplication using bit instructions S. Dandamudi Chapter 12: Page 57 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

58 Two types Fixed-length Variable-length Fixed length strings String Representation Each string uses the same length» Shorter strings are padded (e.g. by blank characters)» Longer strings are truncated Selection of string length is critical» Too large ==> inefficient» Too small ==> truncation of larger strings S. Dandamudi Chapter 12: Page 58 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

59 String Representation (cont d) Variable-length strings Avoids the pitfalls associated with fixed-length strings Two ways of representation Explicitly storing string length (used in PASCAL) string DB Error message str_len DW $-string $ represents the current value of the location counter $ points to the byte after the last character of string Using a sentinel character (used in C)» Uses NULL character Such NULL-terminated strings are called ASCIIZ strings S. Dandamudi Chapter 12: Page 59 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

60 Five string instructions String Instructions LODS LOaD String source STOS STOre String destination MOVS MOVe String source & destination CMPS CoMPare String source & destination SCAS SCAn String destination Specifying operands 32-bit segments: DS:ESI = source operand ES:EDI = destination operand 16-bit segments: DS:SI = source operand ES:DI = destination operand S. Dandamudi Chapter 12: Page 60 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

61 String Instructions (cont d) Each string instruction Can operate on 8-, 16-, or 32-bit operands Updates index register(s) automatically» Byte operands: increment/decrement by 1» Word operands: increment/decrement by 2» Doubleword operands: increment/decrement by 4 Direction flag DF = 0: Forward direction (increments index registers) DF = 1: Backward direction (decrements index registers) Two instructions to manipulate DF std set direction flag (DF = 1) cld clear direction flag (DF = 0) S. Dandamudi Chapter 12: Page 61 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

62 Repetition Prefixes String instructions can be repeated by using a repetition prefix Two types Unconditional repetition rep REPeat Conditional repetition repe/repz REPeat while Equal REPeat while Zero repne/repnz REPeat while Not Equal REPeat while Not Zero S. Dandamudi Chapter 12: Page 62 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

63 Repetition Prefixes (cont d) rep while (CX 0) execute the string instruction CX := CX 1 end while CX register is first checked If zero, string instruction is not executed at all More like the JCXZ instruction S. Dandamudi Chapter 12: Page 63 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

64 repe/repz Repetition Prefixes (cont d) while (CX 0) execute the string instruction CX := CX 1 if (ZF = 0) then exit loop end if end while Useful with cmps and scas string instructions S. Dandamudi Chapter 12: Page 64 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

65 repne/repnz Repetition Prefixes (cont d) while (CX 0) execute the string instruction CX := CX 1 if (ZF = 1) then exit loop end if end while S. Dandamudi Chapter 12: Page 65 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

66 String Move Instructions Three basic instructions movs, lods, and stos Move a string (movs) Format movs dest_string,source_string movsb ; operands are bytes movsw ; operands are words movsd ; operands are doublewords First form is not used frequently Source and destination pointed by DS:(E)SI and ES:(E)DI, respectively S. Dandamudi Chapter 12: Page 66 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

67 String Move Instructions (cont d) movsb --- move a byte string ES:DI := (DS:SI) ; copy a byte if (DF=0) ; forward direction then SI := SI+1 DI := DI+1 else SI := SI 1 DI := DI 1 end if Flags affected: none ; backward direction S. Dandamudi Chapter 12: Page 67 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

68 String Move Instructions (cont d) Example.DATA string1 DB 'The original string',0 strlen EQU $ - string1 string2 DB 80 DUP (?).CODE.STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov CX,strLen ; strlen includes NULL mov SI,OFFSET string1 mov DI,OFFSET string2 cld ; forward direction rep movsb S. Dandamudi Chapter 12: Page 68 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

69 String Move Instructions (cont d) Load a String (LODS) Copies the value from the source string at DS:(E)SI to AL (lodsb) AX (lodsw) EAX (lodsd) Repetition prefix does not make sense It leaves only the last value in AL, AX, or EAX register S. Dandamudi Chapter 12: Page 69 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

70 String Move Instructions (cont d) lodsb --- load a byte string AL := (DS:SI) ; copy a byte if (DF=0) ; forward direction then SI := SI+1 else SI := SI 1 end if Flags affected: none ; backward direction S. Dandamudi Chapter 12: Page 70 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

71 String Move Instructions (cont d) Store a String (STOS) Performs the complementary operation Copies the value in»al (lodsb)»ax (lodsw)» EAX (lodsd) to the destination string at ES:(E)DI Repetition prefix can be used to initialize a block of memory S. Dandamudi Chapter 12: Page 71 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

72 String Move Instructions (cont d) stosb --- store a byte string ES:DI := AL ; copy a byte if (DF=0) ; forward direction then DI := DI+1 else ; backward direction DI := DI 1 end if Flags affected: none S. Dandamudi Chapter 12: Page 72 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

73 String Move Instructions (cont d) Example: Initializes array1 with -1.DATA array1 DW 100 DUP (?).CODE.STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov CX,100 mov DI,OFFSET array1 mov AX,-1 cld ; forward direction rep stosw S. Dandamudi Chapter 12: Page 73 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

74 String Move Instructions (cont d) In general, repeat prefixes are not useful with lods and stos Used in a loop to do conversions while copying mov CX,strLen mov SI,OFFSET string1 mov DI,OFFSET string2 cld ; forward direction loop1: lodsb or AL,20H stosb loop loop1 done: S. Dandamudi Chapter 12: Page 74 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

75 String Compare Instruction cmpsb --- compare two byte strings Compare two bytes at DS:SI and ES:DI and set flags if (DF=0) then SI := SI+1 DI := DI+1 else SI := SI 1 DI := DI 1 end if ; forward direction ; backward direction Flags affected: As per cmp instruction (DS:SI) (ES:DI) S. Dandamudi Chapter 12: Page 75 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

76 String Compare Instruction (cont d).data string1 DB 'abcdfghi',0 strlen EQU $ - string1 string2 DB 'abcdefgh',0.code.startup mov AX,DS ; set up ES mov ES,AX ; to the data segment mov CX,strLen mov SI,OFFSET string1 mov DI,OFFSET string2 cld ; forward direction repe cmpsb dec SI dec DI ; leaves SI & DI pointing to the last character that differs S. Dandamudi Chapter 12: Page 76 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

77 String Compare Instruction (cont d).data string1 DB 'abcdfghi',0 strlen EQU $ - string1-1 string2 DB 'abcdefgh',0.code.startup mov AX,DS ; set up ES mov ES,AX ; to the data segment mov CX,strLen mov SI,OFFSET string1 + strlen - 1 mov DI,OFFSET string2 + strlen - 1 std ; backward direction repne cmpsb inc SI ; Leaves SI & DI pointing to the first character that matches inc DI ; in the backward direction S. Dandamudi Chapter 12: Page 77 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

78 String Scan Instruction scasb --- Scan a byte string Compare AL to the byte at ES:DI and set flags if (DF=0) ; forward direction then DI := DI+1 else ; backward direction DI := DI 1 end if Flags affected: As per cmp instruction (DS:SI)-(ES:DI) scasw uses AX and scasd uses EAX registers instead of AL S. Dandamudi Chapter 12: Page 78 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

79 String Scan Instruction (cont d).data string1 DB 'abcdefgh',0 Example 1 strlen EQU $ - string1.code.startup mov AX,DS ; set up ES mov ES,AX ; to the data segment mov CX,strLen mov DI,OFFSET string1 mov AL,'e' ; character to be searched cld ; forward direction repne scasb dec DI ; leaves DI pointing to e in string1 S. Dandamudi Chapter 12: Page 79 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

80 String Scan Instruction (cont d).data string1 DB ' abc',0 strlen EQU $ - string1.code.startup mov AX,DS ; set up ES Example 2 mov ES,AX ; to the data segment mov CX,strLen mov DI,OFFSET string1 mov AL,' ' ; character to be searched cld ; forward direction repe scasb dec DI ; leaves DI pointing to the first non-blank character a S. Dandamudi Chapter 12: Page 80 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

81 Illustrative Examples LDS and LES instructions String pointer can be loaded into DS/SI or ES/DI register pair by using lds or les instructions Syntax lds register,source les register,source register should be a 16-bit register source is a pointer to a 32-bit memory operand register is typically SI in lds and DI in les S. Dandamudi Chapter 12: Page 81 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

82 Illustrative Examples (cont d) Actions of lds and les lds les register := (source) DS := (source+2) register := (source) ES := (source+2) Pentium also supports lfs, lgs, and lss to load the other segment registers S. Dandamudi Chapter 12: Page 82 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

83 Illustrative Examples (cont d) Seven popular string processing routines are given as examples in string.asm str_len str_mov str-cpy str_cat str_cmp str_chr str_cnv Given in the text S. Dandamudi Chapter 12: Page 83 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

84 Indirect Procedure Call Direct procedure calls specify the offset of the first instruction of the called procedure In indirect procedure call, the offset is specified through memory or a register If BX contains pointer to the procedure, we can use call BX If the word in memory at target_proc_ptr contains the offset of the called procedure, we can use call target_proc_ptr These are similar to direct and indirect jumps Last slide S. Dandamudi Chapter 12: Page 84 To be used with S. Dandamudi, Fundamentals of Computer Organization and Design, Springer,.

String Processing. Chapter 9 S. Dandamudi

String Processing. Chapter 9 S. Dandamudi String Processing Chapter 9 S. Dandamudi Outline String representation Using string length Using a sentinel character String instructions Repetition prefixes Direction flag String move instructions String

More information

Selection and Iteration. Chapter 7 S. Dandamudi

Selection and Iteration. Chapter 7 S. Dandamudi Selection and Iteration Chapter 7 S. Dandamudi Outline Unconditional jump Compare instruction Conditional jumps Single flags Unsigned comparisons Signed comparisons Loop instructions Implementing high-level

More information

8086 INSTRUCTION SET

8086 INSTRUCTION SET 8086 INSTRUCTION SET Complete 8086 instruction set Quick reference: AAA AAD AAM AAS ADC ADD AND CALL CBW CLC CLD CLI CMC CMP CMPSB CMPSW CWD DAA DAS DEC DIV HLT IDIV IMUL IN INC INT INTO I JA JAE JB JBE

More information

Logical and bit operations

Logical and bit operations Assembler lecture 6 S.Šimoňák, DCI FEEI TU of Košice Logical and bit operations instructions performing logical operations, shifts and rotations logical expressions and bit manipulations strings Logical

More information

3.1 DATA MOVEMENT INSTRUCTIONS 45

3.1 DATA MOVEMENT INSTRUCTIONS 45 3.1.1 General-Purpose Data Movement s 45 3.1.2 Stack Manipulation... 46 3.1.3 Type Conversion... 48 3.2.1 Addition and Subtraction... 51 3.1 DATA MOVEMENT INSTRUCTIONS 45 MOV (Move) transfers a byte, word,

More information

APPENDIX C INSTRUCTION SET DESCRIPTIONS

APPENDIX C INSTRUCTION SET DESCRIPTIONS APPENDIX C INSTRUCTION SET DESCRIPTIONS This appendix provides reference information for the 80C186 Modular Core family instruction set. Tables C-1 through C-3 define the variables used in Table C-4, which

More information

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS 1. Object of laboratory The x86 microprocessor family has a large variety of instructions that allow instruction flow control. We have 4 categories: jump,

More information

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM EXPERIMENT WRITE UP AIM: Assembly language program to search a number in given array. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

More information

Intel 8086: Instruction Set

Intel 8086: Instruction Set IUST-EE (Chapter 6) Intel 8086: Instruction Set 1 Outline Instruction Set Data Transfer Instructions Arithmetic Instructions Bit Manipulation Instructions String Instructions Unconditional Transfer Instruction

More information

Assembler lecture 4 S.Šimoňák, DCI FEEI TU of Košice

Assembler lecture 4 S.Šimoňák, DCI FEEI TU of Košice Assembler lecture 4 S.Šimoňák, DCI FEEI TU of Košice Addressing data access specification arrays - specification and manipulation impacts of addressing to performance Processor architecture CISC (more

More information

US06CCSC04: Introduction to Microprocessors and Assembly Language UNIT 3: Assembly Language Instructions II

US06CCSC04: Introduction to Microprocessors and Assembly Language UNIT 3: Assembly Language Instructions II Unconditional & Conditional JUMP instructions: Conditional JUMP instructions: JA/JNBE Jump if above / Jump if not Below or Equal These two mnemonics represent the same instruction. The term above and below

More information

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB4 FÖYÜ

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB4 FÖYÜ LAB4 RELATED INSTRUCTIONS: Compare, division and jump instructions CMP REG, memory memory, REG REG, REG memory, immediate REG, immediate operand1 - operand2 Result is not stored anywhere, flags are set

More information

CS-202 Microprocessor and Assembly Language

CS-202 Microprocessor and Assembly Language CS-202 Microprocessor and Assembly Language Lecture 2 Introduction to 8086 Assembly Language Dr Hashim Ali Spring - 2019 Department of Computer Science and Engineering HITEC University Taxila!1 Lecture

More information

Week /8086 Microprocessor Programming I

Week /8086 Microprocessor Programming I Week 4 8088/8086 Microprocessor Programming I Example. The PC Typewriter Write an 80x86 program to input keystrokes from the PC s keyboard and display the characters on the system monitor. Pressing any

More information

EC 333 Microprocessor and Interfacing Techniques (3+1)

EC 333 Microprocessor and Interfacing Techniques (3+1) EC 333 Microprocessor and Interfacing Techniques (3+1) Lecture 6 8086/88 Microprocessor Programming (Arithmetic Instructions) Dr Hashim Ali Fall 2018 Department of Computer Science and Engineering HITEC

More information

Conditional Processing

Conditional Processing ١ Conditional Processing Computer Organization & Assembly Language Programming Dr Adnan Gutub aagutub at uqu.edu.sa Presentation Outline [Adapted from slides of Dr. Kip Irvine: Assembly Language for Intel-Based

More information

UNIT II 16 BIT MICROPROCESSOR INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING. The Intel 8086 Instruction Set

UNIT II 16 BIT MICROPROCESSOR INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING. The Intel 8086 Instruction Set UNIT II 16 BIT MICROPROCESSOR INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING The Intel 8086 Instruction Set 1.Explain Addressing Modes of 8086? ADDRESSING MODES Implied - the data value/data address

More information

IFE: Course in Low Level Programing. Lecture 6

IFE: Course in Low Level Programing. Lecture 6 IFE: Course in Low Level Programing Lecture 6 Instruction Set of Intel x86 Microprocessors Conditional jumps Jcc jump on condition cc, JMP jump always, CALL call a procedure, RET return from procedure,

More information

PESIT Bangalore South Campus

PESIT Bangalore South Campus INTERNAL ASSESSMENT TEST 2 Date : 02/04/2018 Max Marks: 40 Subject & Code : Microprocessor (15CS44) Section : IV A and B Name of faculty: Deepti.C Time : 8:30 am-10:00 am Note: Note: Answer any five complete

More information

Chapter 12. Selected Pentium Instructions

Chapter 12. Selected Pentium Instructions Chapter 12 Selected Pentium Instructions 1 2 Chapter 12 12 1 Carry flag indicates out-of-range error for unsigned operations. Chapter 12 3 12 2 Overflow flag indicates out-of-range error for signed operations.

More information

Week /8086 Microprocessor Programming

Week /8086 Microprocessor Programming Week 5 8088/8086 Microprocessor Programming Multiplication and Division Multiplication Multiplicant Operand Result (MUL or IMUL) (Multiplier) Byte * Byte AL Register or memory Word * Word AX Register or

More information

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI Note: PUSHF / POPF have no operands The figure below shows that if (SS) = 3000H, (SP) = 0042H, so the execution of POP CX loads CX by the word 4050H form the stack segment. The SP is incremented by 2.

More information

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans. INSTRUCTOR: ABDULMUTTALIB A H ALDOURI Conditional Jump Cond Unsigned Signed = JE : Jump Equal JE : Jump Equal ZF = 1 JZ : Jump Zero JZ : Jump Zero ZF = 1 JNZ : Jump Not Zero JNZ : Jump Not Zero ZF = 0

More information

Logical and Bit Operations. Chapter 8 S. Dandamudi

Logical and Bit Operations. Chapter 8 S. Dandamudi Logical and Bit Operations Chapter 8 S. Dandamudi Outline Logical instructions AND OR XOR NOT TEST Shift instructions Logical shift instructions Arithmetic shift instructions Rotate instructions Rotate

More information

Defining and Using Simple Data Types

Defining and Using Simple Data Types 85 CHAPTER 4 Defining and Using Simple Data Types This chapter covers the concepts essential for working with simple data types in assembly-language programs The first section shows how to declare integer

More information

Arithmetic and Logic Instructions And Programs

Arithmetic and Logic Instructions And Programs Dec Hex Bin 3 3 00000011 ORG ; FOUR Arithmetic and Logic Instructions And Programs OBJECTIVES this chapter enables the student to: Demonstrate how 8-bit and 16-bit unsigned numbers are added in the x86.

More information

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10] The following pages contain references for use during the exam: tables containing the x86 instruction set (covered so far) and condition codes. You do not need to submit these pages when you finish your

More information

Basic Assembly Instructions

Basic Assembly Instructions Basic Assembly Instructions Ned Nedialkov McMaster University Canada SE 3F03 January 2013 Outline Multiplication Division FLAGS register Branch Instructions If statements Loop instructions 2/21 Multiplication

More information

Lab 6: Conditional Processing

Lab 6: Conditional Processing COE 205 Lab Manual Lab 6: Conditional Processing Page 56 Lab 6: Conditional Processing Contents 6.1. Unconditional Jump 6.2. The Compare Instruction 6.3. Conditional Jump Instructions 6.4. Finding the

More information

Arithmetic Instructions

Arithmetic Instructions Segment 3C Arithmetic Instructions This topic covers the following instructions: Addition (ADD, INC, ADC) Subtraction (SUB, DEC, SBB,CMP) Multiplication (MUL, IMUL) Division (DIV, IDIV) BCD Arithmetic

More information

CS401 Assembly Language Solved MCQS From Midterm Papers

CS401 Assembly Language Solved MCQS From Midterm Papers CS401 Assembly Language Solved MCQS From Midterm Papers May 14,2011 MC100401285 Moaaz.pk@gmail.com MC100401285@gmail.com PSMD01(IEMS) Question No:1 ( Marks: 1 ) - Please choose one The first instruction

More information

Am186 and Am188 Family Instruction Set Manual. February, 1997

Am186 and Am188 Family Instruction Set Manual. February, 1997 Am186 and Am188 Family Instruction Set Manual February, 1997 1997 Advanced Micro Devices, Inc. Advanced Micro Devices reserves the right to make changes in its products without notice in order to improve

More information

Instructions moving data

Instructions moving data do not affect flags. Instructions moving data mov register/mem, register/mem/number (move data) The difference between the value and the address of a variable mov al,sum; value 56h al mov ebx,offset Sum;

More information

Q1: Multiple choice / 20 Q2: Protected mode memory accesses

Q1: Multiple choice / 20 Q2: Protected mode memory accesses 16.317: Microprocessor-Based Systems I Summer 2012 Exam 2 August 1, 2012 Name: ID #: For this exam, you may use a calculator and one 8.5 x 11 double-sided page of notes. All other electronic devices (e.g.,

More information

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY BACKGROUND 8086 CPU has 8 general purpose registers listed below: AX - the accumulator register (divided into AH / AL): 1. Generates shortest machine code 2. Arithmetic, logic and data transfer 3. One

More information

Computer Architecture..Second Year (Sem.2).Lecture(4) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

Computer Architecture..Second Year (Sem.2).Lecture(4) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات مدرس المادة : م. سندس العزاوي... قسم / الحاسبات... - 26 27 Assembly Level Machine Organization Usage of AND, OR, XOR, NOT AND : X Y X AND Y USE : to chick any bit by change ( to ) or ( to ) EX : AX = FF5

More information

Summer 2003 Lecture 4 06/14/03

Summer 2003 Lecture 4 06/14/03 Summer 2003 Lecture 4 06/14/03 LDS/LES/LSS General forms: lds reg,mem lseg reg,mem Load far pointer ~~ outside of current segment {E.g., load reg w/value @ mem, & seg w/mem+2 XCHG Exchange values General

More information

CMSC 313 Lecture 05 [draft]

CMSC 313 Lecture 05 [draft] CMSC 313 Lecture 05 [draft] More on Conditional Jump Instructions Short Jumps vs Near Jumps Using Jump Instructions Logical (bit manipulation) Instructions AND, OR, NOT, SHL, SHR, SAL, SAR, ROL, ROR, RCL,

More information

EEM336 Microprocessors I. Arithmetic and Logic Instructions

EEM336 Microprocessors I. Arithmetic and Logic Instructions EEM336 Microprocessors I Arithmetic and Logic Instructions Introduction We examine the arithmetic and logic instructions. The arithmetic instructions include addition, subtraction, multiplication, division,

More information

Introduction to 8086 Assembly

Introduction to 8086 Assembly Introduction to 8086 Assembly Lecture 5 Jump, Conditional Jump, Looping, Compare instructions Labels and jumping (the jmp instruction) mov eax, 1 add eax, eax jmp label1 xor eax, eax label1: sub eax, 303

More information

Jump instructions. Unconditional jumps Direct jump. do not change flags. jmp label

Jump instructions. Unconditional jumps Direct jump. do not change flags. jmp label do not change flags Unconditional jumps Direct jump jmp label Jump instructions jmp Continue xor eax,eax Continue: xor ecx,ecx Machine code: 0040340A EB 02 0040340C 33 C0 0040340E 33 C9 displacement =

More information

Assignment no:4 on chapter no :3 : Instruction set of 8086

Assignment no:4 on chapter no :3 : Instruction set of 8086 Assignment no:4 on chapter no :3 : Instruction set of 8086 1) Describe any two string operation instruction of 8086 with syntax & one example of each. 1] REP: REP is a prefix which is written before one

More information

Experiment N o 8. String Handling Instructions

Experiment N o 8. String Handling Instructions Experiment N o 8 String Handling Instructions Introduction: In this experiment you will deal with string handling instructions, such as reading a string, moving a string from one memory location to another,

More information

Lecture Notes on Assembly Language - J. Vaughan

Lecture Notes on Assembly Language - J. Vaughan Lecture Notes on Assembly Language - 18. Shifts and Rotates SHL, SHR: Bitwise Logical Shifts SHL r/m8,1 ; D0 /4 SHL r/m8,cl ; D2 /4 SHL r/m8,imm8 SHL r/m16,1 ; C0 /4 ib ; o16 D1 /4 SHL r/m16,cl ; o16 D3

More information

from WRITE GREAT CODE Volume 2: Thinking Low-Level, Writing High-Level ONLINE APPENDIX A The Minimal 80x86 Instruction Set by Randall Hyde

from WRITE GREAT CODE Volume 2: Thinking Low-Level, Writing High-Level ONLINE APPENDIX A The Minimal 80x86 Instruction Set by Randall Hyde from WRITE GREAT CODE Volume 2: Thinking Low-Level, Writing High-Level ONLINE APPENDIX A The Minimal 80x86 Set by Randall Hyde San Francisco WRITE GREAT CODE, Volume 2. Copyright 2006 by Randall Hyde.

More information

Inline Assembler. Willi-Hans Steeb and Yorick Hardy. International School for Scientific Computing

Inline Assembler. Willi-Hans Steeb and Yorick Hardy. International School for Scientific Computing Inline Assembler Willi-Hans Steeb and Yorick Hardy International School for Scientific Computing e-mail: steebwilli@gmail.com Abstract We provide a collection of inline assembler programs. 1 Using the

More information

8088/8086 Programming Integer Instructions and Computations

8088/8086 Programming Integer Instructions and Computations Unit3 reference 2 8088/8086 Programming Integer Instructions and Computations Introduction Up to this point we have studied the software architecture of the 8088 and 8086 microprocessors, their instruction

More information

TUTORIAL. Emulador Emu8086 do. Microprocessador 8086

TUTORIAL. Emulador Emu8086 do. Microprocessador 8086 1 TUTORIAL Emulador Emu8086 do Microprocessador 8086 2 8086 Assembler Tutorial for Beginners (Part 1) This tutorial is intended for those who are not familiar with assembler at all, or have a very distant

More information

CS401 Assembly Language Solved Subjective MAY 03,2012 From Midterm Papers. MC

CS401 Assembly Language Solved Subjective MAY 03,2012 From Midterm Papers. MC CS401 Assembly Language Solved Subjective MAY 03,2012 From Midterm Papers MC100401285 Moaaz.pk@gmail.com Mc100401285@gmail.com PSMD01 MIDTERM FALL 2011 CS401 Assembly Language Q: Affected flag of AND operation

More information

Assembly Language LAB

Assembly Language LAB Assembly Language LAB Islamic University Gaza Engineering Faculty Department of Computer Engineering 2013 ECOM 2125: Assembly Language LAB Created by: Eng. Ahmed M. Ayash Modified and Presented By: Eihab

More information

We will first study the basic instructions for doing multiplications and divisions

We will first study the basic instructions for doing multiplications and divisions MULTIPLICATION, DIVISION AND NUMERICAL CONVERSIONS We will first study the basic instructions for doing multiplications and divisions We then use these instructions to 1. Convert a string of ASCII digits

More information

UNIT III MICROPROCESSORS AND MICROCONTROLLERS MATERIAL OVERVIEW: Addressing Modes of Assembler Directives. Procedures and Macros

UNIT III MICROPROCESSORS AND MICROCONTROLLERS MATERIAL OVERVIEW: Addressing Modes of Assembler Directives. Procedures and Macros OVERVIEW: UNIT III Addressing Modes of 8086 Assembler Directives Procedures and Macros Instruction Set of 8086 Data Transfer Group Arithmetic Group Logical Instructions Rotate and Shift instructions Loop

More information

Kingdom of Saudi Arabia Ministry of Higher Education. Taif University. Faculty of Computers & Information Systems

Kingdom of Saudi Arabia Ministry of Higher Education. Taif University. Faculty of Computers & Information Systems Kingdom of Saudi Arabia Ministry of Higher Education Taif University Faculty of Computers & Information Systems المملكة العربية السعودية وزارة التعليم العالي جامعة الطاي ف آلية الحاسبات ونظم المعلومات

More information

Week /8086 Microprocessor Programming II

Week /8086 Microprocessor Programming II Week 5 8088/8086 Microprocessor Programming II Quick Review Shift & Rotate C Target register or memory SHL/SAL 0 C SHR 0 SAR C Sign Bit 2 Examples Examples Ex. Ex. Ex. SHL dest, 1; SHL dest,cl; SHL dest,

More information

Program controlled semiconductor device (IC) which fetches (from memory), decodes and executes instructions.

Program controlled semiconductor device (IC) which fetches (from memory), decodes and executes instructions. 2 Microprocessor Program controlled semiconductor device (IC) which fetches (from memory), decodes and executes instructions. It is used as CPU (Central Processing Unit) in computers. 3 Microprocessor

More information

ECOM Computer Organization and Assembly Language. Computer Engineering Department CHAPTER 7. Integer Arithmetic

ECOM Computer Organization and Assembly Language. Computer Engineering Department CHAPTER 7. Integer Arithmetic ECOM 2325 Computer Organization and Assembly Language Computer Engineering Department CHAPTER 7 Integer Arithmetic Presentation Outline Shift and Rotate Instructions Shift and Rotate Applications Multiplication

More information

Intel Instruction Set (gas)

Intel Instruction Set (gas) Intel Instruction Set (gas) These slides provide the gas format for a subset of the Intel processor instruction set, including: Operation Mnemonic Name of Operation Syntax Operation Examples Effect on

More information

Lecture (08) x86 programming 7

Lecture (08) x86 programming 7 Lecture (08) x86 programming 7 By: Dr. Ahmed ElShafee 1 Conditional jump: Conditional jumps are executed only if the specified conditions are true. Usually the condition specified by a conditional jump

More information

if 2 16bit operands multiplied the result will be

if 2 16bit operands multiplied the result will be how many operands in ADC? ans:3 how 32 bit word is defined? ans define double if 2 16bit operands multiplied the result will be ans 32bit if div by ero occurs then?? ans div by zero int for software int

More information

Assembler lecture 5 S.Šimoňák, DCI FEEI TU of Košice

Assembler lecture 5 S.Šimoňák, DCI FEEI TU of Košice Assembler lecture 5 S.Šimoňák, DCI FEEI TU of Košice Jumps and iterations conditional and unconditional jumps iterations (loop) implementation of HLL control structures Unconditional jump unconditional

More information

Assembly Language for Intel-Based Computers, 5 th Edition. Chapter 9: Strings and Arrays

Assembly Language for Intel-Based Computers, 5 th Edition. Chapter 9: Strings and Arrays Assembly Language for Intel-Based Computers, 5 th Edition Kip R. Irvine Chapter 9: Strings and Arrays Slide show prepared by the author Revision date: June 4, 2006 (c) Pearson Education, 2006-2007. All

More information

Chapter Four Instructions Set

Chapter Four Instructions Set Chapter Four Instructions set Instructions set 8086 has 117 instructions, these instructions divided into 6 groups: 1. Data transfer instructions 2. Arithmetic instructions 3. Logic instructions 4. Shift

More information

Mnem. Meaning Format Operation Flags affected ADD Addition ADD D,S (D) (S)+(D) (CF) Carry ADC Add with ADC D,C (D) (S)+(D)+(CF) O,S,Z,A,P,C

Mnem. Meaning Format Operation Flags affected ADD Addition ADD D,S (D) (S)+(D) (CF) Carry ADC Add with ADC D,C (D) (S)+(D)+(CF) O,S,Z,A,P,C ARITHMETIC AND LOGICAL GROUPS 6-1 Arithmetic and logical groups: The arithmetic group includes instructions for the addition, subtraction, multiplication, and division operations. The state that results

More information

UNIT III 8086 Microprocessor. Lecture 1

UNIT III 8086 Microprocessor. Lecture 1 UNIT III 8086 Microprocessor Lecture 1 Features of 8086 Microprocessor (RGPV 2013) The 8086, announced in 1978, was the first 16-bit microprocessor introduced by Intel Corporation. 1) 8086 has 16-bit ALU;

More information

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB Lab # 9 Integer Arithmetic and Bit Manipulation April, 2014 1 Assembly Language LAB Bitwise

More information

Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION 80x86 Instructions

Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION 80x86 Instructions 80x86 Instructions Chapter 4 In the following sections, we review some basic instructions used by the 80x86 architecture. This Jones is by & no Bartlett means a Learning, complete list LLC of the Intel

More information

Signed number Arithmetic. Negative number is represented as

Signed number Arithmetic. Negative number is represented as Signed number Arithmetic Signed and Unsigned Numbers An 8 bit number system can be used to create 256 combinations (from 0 to 255), and the first 128 combinations (0 to 127) represent positive numbers

More information

CHAPTER SEVENTEEN Assemblers versus Compilers. Intel 80x86 Assembly Language

CHAPTER SEVENTEEN Assemblers versus Compilers. Intel 80x86 Assembly Language CHAPTER SEVENTEEN Intel 80x86 Assembly Language In Chapter 15, we developed a generic assembly language and its associated machine code. This language was presented to create a few simple programs and

More information

Code segment Stack segment

Code segment Stack segment Registers Most of the registers contain data/instruction offsets within 64 KB memory segment. There are four different 64 KB segments for instructions, stack, data and extra data. To specify where in 1

More information

Branching and Looping

Branching and Looping Branching and Looping Ray Seyfarth August 10, 2011 Branching and looping So far we have only written straight line code Conditional moves helped spice things up In addition conditional moves kept the pipeline

More information

Assembly Language Tutorial

Assembly Language Tutorial Assembly Language Tutorial Introduction What's new in the Assembler material After of one year that we've released the first Assembler material on-line. We've received a lot of e- mail where each people

More information

Branching and Looping

Branching and Looping Branching and Looping EECE416 uc Fall 2011 Unconditional Jumps jmp Like a goto in a high-level language Format: jmp StatementLabel The next statement executed will be the one at StatementLabel: jmp Encoding

More information

Integer Arithmetic. Pu-Jen Cheng. Adapted from the slides prepared by Kip Irvine for the book, Assembly Language for Intel-Based Computers, 5th Ed.

Integer Arithmetic. Pu-Jen Cheng. Adapted from the slides prepared by Kip Irvine for the book, Assembly Language for Intel-Based Computers, 5th Ed. Computer Organization & Assembly Languages Integer Arithmetic Pu-Jen Cheng Adapted from the slides prepared by Kip Irvine for the book, Assembly Language for Intel-Based Computers, 5th Ed. Chapter Overview

More information

9/25/ Software & Hardware Architecture

9/25/ Software & Hardware Architecture 8086 Software & Hardware Architecture 1 INTRODUCTION It is a multipurpose programmable clock drive register based integrated electronic device, that reads binary instructions from a storage device called

More information

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string. 1 od 5 17. 12. 2017 23:53 (https://github.com/schweigi/assembler-simulator) Introduction This simulator provides a simplified assembler syntax (based on NASM (http://www.nasm.us)) and is simulating a x86

More information

Q1: Multiple choice / 20 Q2: Memory addressing / 40 Q3: Assembly language / 40 TOTAL SCORE / 100

Q1: Multiple choice / 20 Q2: Memory addressing / 40 Q3: Assembly language / 40 TOTAL SCORE / 100 16.317: Microprocessor-Based Systems I Summer 2012 Exam 1 July 20, 2012 Name: ID #: For this exam, you may use a calculator and one 8.5 x 11 double-sided page of notes. All other electronic devices (e.g.,

More information

IBM PC Hardware CPU 8088, Pentium... ALU (Arithmetic and Logic Unit) Registers. CU (Control Unit) IP.

IBM PC Hardware CPU 8088, Pentium... ALU (Arithmetic and Logic Unit) Registers. CU (Control Unit) IP. IBM PC Hardware CPU 8088, 8086 80286 80386 80486 Pentium... ALU (Arithmetic and Logic Unit) Registers CU (Control Unit) IP Memory ROM BIOS I/O RAM OS Programs Video memory BIOS data Interrupt Vectors Memory

More information

Lecture 9. INC and DEC. INC/DEC Examples ADD. Arithmetic Operations Overflow Multiply and Divide

Lecture 9. INC and DEC. INC/DEC Examples ADD. Arithmetic Operations Overflow Multiply and Divide Lecture 9 INC and DEC Arithmetic Operations Overflow Multiply and Divide INC adds one to a single operand DEC decrements one from a single operand INC destination DEC destination where destination can

More information

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI 8 Unsigned and Signed Integer Numbers 1. Unsigned integer numbers: each type of integer can be either byte-wide or word-wide. This data type can be used to represent decimal numbers in the range 0 through

More information

Basic Execution Environment

Basic Execution Environment Basic Execution Environment 3 CHAPTER 3 BASIC EXECUTION ENVIRONMENT This chapter describes the basic execution environment of an Intel Architecture processor as seen by assembly-language programmers.

More information

8086 ASSEMBLY LANGUAGE PROGRAMMING

8086 ASSEMBLY LANGUAGE PROGRAMMING UNIT-II 8086 ASSEMBLY LANGUAGE PROGRAMMING Contents at a glance: 8086 Instruction Set Assembler directives Procedures and macros. 8086 MEMORY INTERFACING: 8086 addressing and address decoding Interfacing

More information

ECE 498 Linux Assembly Language Lecture 3

ECE 498 Linux Assembly Language Lecture 3 ECE 498 Linux Assembly Language Lecture 3 Vince Weaver http://www.eece.maine.edu/ vweaver vincent.weaver@maine.edu 20 November 2012 Statically Linked C Hello-World Disassembly of section.text : 08048320

More information

ASSEMBLY LANGUAGE PROGRAMMING OF THE MICROCOMPUTER

ASSEMBLY LANGUAGE PROGRAMMING OF THE MICROCOMPUTER CHAPTER ASSEMBLY LANGUAGE PROGRAMMING OF THE MICROCOMPUTER 2.1 Introduction To run a program, a microcomputer must have the program stored in binary form in successive memory locations. There are three

More information

Lecture (07) x86 programming 6

Lecture (07) x86 programming 6 Lecture (07) x86 programming 6 By: Dr. Ahmed ElShafee 1 The Flag Register 31 21 20 19 18 17 16 14 13 12 11 10 9 8 7 6 4 2 0 ID VIP VIF AC VM RF NT IOP 1 IOP 0 O D I T S Z A P C 8088/8086 80286 80386 80486

More information

Lecture 5 Program Logic and Control

Lecture 5 Program Logic and Control Lecture 5 Program Logic and Control Chapter Outline Short, near and far address JMP Instruction The CMP Instruction Conditional Jump instruction The Loop instruction While Loop REPEAT Loop Short,near,and

More information

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY BACKGROUND Segment The "SEGMENT" and "ENDS" directives indicate to the assembler the beginning and ending of a segment and have the following format label SEGMENT [options] ;place the statements belonging

More information

Basic Assembly SYSC-3006

Basic Assembly SYSC-3006 Basic Assembly Program Development Problem: convert ideas into executing program (binary image in memory) Program Development Process: tools to provide people-friendly way to do it. Tool chain: 1. Programming

More information

M80C286 HIGH PERFORMANCE CHMOS MICROPROCESSOR WITH MEMORY MANAGEMENT AND PROTECTION

M80C286 HIGH PERFORMANCE CHMOS MICROPROCESSOR WITH MEMORY MANAGEMENT AND PROTECTION HIGH PERFORMANCE CHMOS MICROPROCESSOR WITH MEMORY MANAGEMENT AND PROTECTION Military Y High Speed CHMOS III Technology Pin for Pin Clock for Clock and Functionally Compatible with the HMOS M80286 Y 10

More information

EEM336 Microprocessors I. Data Movement Instructions

EEM336 Microprocessors I. Data Movement Instructions EEM336 Microprocessors I Data Movement Instructions Introduction This chapter concentrates on common data movement instructions. 2 Chapter Objectives Upon completion of this chapter, you will be able to:

More information

Section 001. Read this before starting!

Section 001. Read this before starting! Points missed: Student's Name: Total score: /100 points East Tennessee State University Department of Computer and Information Sciences CSCI 2150 (Tarnoff) Computer Organization TEST 3 for Fall Semester,

More information

Hands on Experience for Faculty in Laboratories Phase I JNTUK, Kakinada

Hands on Experience for Faculty in Laboratories Phase I JNTUK, Kakinada Hands on Experience for Faculty in Laboratories Phase I JNTUK, Kakinada Cover page Report Details of College Name of the College College Code and District Name of the Principal Contact No s Lendi Institute

More information

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input.

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input. 22 Assembly Language for Intel-Based Computers, 4th Edition 6.6 Application: Finite-State Machines 1. A directed graph (also known as a diagraph). 2. Each node is a state. 3. Each edge is a transition

More information

Chapter 6 (Part a) Conditional Processing

Chapter 6 (Part a) Conditional Processing Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2025: Assembly Language Discussion Chapter 6 (Part a) Conditional Processing Eng. Eman R. Habib April, 2014 2 Assembly

More information

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313) Philadelphia University Faculty of Engineering Marking Scheme Examination Paper Department of CE Module: Microprocessors (630313) Final Exam Second Semester Date: 02/06/2018 Section 1 Weighting 40% of

More information

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

X86 Addressing Modes Chapter 3 Review: Instructions to Recognize X86 Addressing Modes Chapter 3" Review: Instructions to Recognize" 1 Arithmetic Instructions (1)! Two Operand Instructions" ADD Dest, Src Dest = Dest + Src SUB Dest, Src Dest = Dest - Src MUL Dest, Src

More information

Assembly Language Lab # 6

Assembly Language Lab # 6 Faculty of Engineering Computer Engineering Department Islamic University of Gaza 2011 Assembly Language Lab # 6 Boolean Instructions and Conditional Structured Eng. Doaa Abu Jabal Objective: Assembly

More information

SRI VENKATESWARA COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ECE EC6504 MICROPROCESSOR AND MICROCONTROLLER (REGULATION 2013)

SRI VENKATESWARA COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ECE EC6504 MICROPROCESSOR AND MICROCONTROLLER (REGULATION 2013) SRI VENKATESWARA COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ECE EC6504 MICROPROCESSOR AND MICROCONTROLLER (REGULATION 2013) UNIT I THE 8086 MICROPROCESSOR PART A (2 MARKS) 1. What are the functional

More information

16.317: Microprocessor Systems Design I Fall 2013

16.317: Microprocessor Systems Design I Fall 2013 16.317: Microprocessor Systems Design I Fall 2013 Exam 1 Solution 1. (20 points, 5 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling

More information

db "Please enter up to 256 characters (press Enter Key to finish): ",0dh,0ah,'$'

db Please enter up to 256 characters (press Enter Key to finish): ,0dh,0ah,'$' PA4 Sample Solution.model large.stack 100h.data msg1 db "This programs scans a string of up to 256 bytes and counts the repetitions of the number 4206 and sums them.",0dh,0ah,'$' msg2 db "Please enter

More information

Assembly Language: IA-32 Instructions

Assembly Language: IA-32 Instructions Assembly Language: IA-32 Instructions 1 Goals of this Lecture Help you learn how to: Manipulate data of various sizes Leverage more sophisticated addressing modes Use condition codes and jumps to change

More information