Presentation is loading. Please wait.

Presentation is loading. Please wait.

11.1/36 Repeat: From Bits and Pieces Till Strings.

Similar presentations


Presentation on theme: "11.1/36 Repeat: From Bits and Pieces Till Strings."— Presentation transcript:

1 11.1/36 Repeat: From Bits and Pieces Till Strings.

2 11.2/36 Overview Bit, Nibble, Byte, Word & Double Word, Decimal, Binary & Hex, ASCII, Unsigned, Signed numbers & 2’complement, Logic Commands and masking, Shift Commands, String Commands.

3 11.3/36 Bit, Nibble, Byte, Word & Double Word, Decimal, Binary & Hex, ASCII, Unsigned, Signed numbers & 2’complement, Logic Commands and masking, Shift Commands, String Commands.

4 11.4/36 Bit, Nibble, Byte, Word & Double Word Bit – The smallest information unit. Either 0 or 1, Nibble – four bits: from 0000 to 1111, Byte – eight bits from 00000000 to 11111111, Word – sixteen bits: from 0000000000000000 to 1111111111111111, Double Word – thirty two bits: from 00000000000000000000000000000000 to 11111111111111111111111111111111.

5 11.5/36 Bit, Nibble, Byte, Word & Double Word Nibble – 0110, Byte – 11011010, Word – 0100111000111111, Double Word – 11000111000011111001111000100110. MSB LSB

6 11.6/36 Bit, Nibble, Byte, Word & Double Word, Decimal, Binary & Hex, ASCII, Unsigned, Signed numbers & 2’complement, Logic Commands and masking, Shift Commands, String Commands.

7 11.7/36 Decimal, Binary & Hex … Regularly, we look upon numbers as they are represented in the decimal base: We got ten digits: 1, 2, 3, 4, 5, 6, 7, 8, 9 and 0, In the binary base we got two digits: 0 and 1, In the hexadecimal base we got sixteen digits: 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, and 0, We may convert numbers from each base to other.

8 11.8/36 Decimal, Binary & Hex (Cont’d) DecBinaryHex 0 0 0 1 1 1 2 10 2 3 11 3 4 100 4 5 101 5 6 110 6 7 111 7 DecBinaryHex 8 1000 8 9 1001 9 10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F 161 000010

9 11.9/36 Bit, Nibble, Byte, Word & Double Word, Decimal, Binary & Hex, ASCII, Unsigned, Signed numbers & 2’complement, Logic Commands and masking, Shift Commands, String Commands.

10 11.10/36 ASCII Code … ASCII is an acronym of American Standard Code for Information Interchange, This code assigns the letters of the alphabet, decimal digits from 0 to 9 and some additional symbols a binary number of 7 bits, putting the 8th bit in its off state or 0, This way each letter, digit or special character occupies one byte in the computer memory.

11 11.11/36 ASCII Code (Cont’d) 32H 46H 66H

12 11.12/36 Bit, Nibble, Byte, Word & Double Word, Decimal, Binary & Hex, ASCII, Unsigned, Signed numbers & 2’complement, Logic Commands and masking, Shift Commands, String Commands.

13 11.13/36 Unsigned, Signed numbers & 2’comp. Unsigned numbers: 0000 B = 0 10, 0011 B = 3 10, 1010 B = 10 10, 1110 B = 14 10, Only ‘Positive’ numbers, Signed numbers: Gives us the ability to mark numbers as positive or negative, What about sign and magnitude – the first bit will be dedicated to hold the number sign: 0 – Positive, 1 – Negative,

14 11.14/36 2’complement … But than – we got two zeros: 0000 B = The positive 0, 1000 B = The negative 0, So we got the 2’complement representation: 0011 B = 3 10, First, invert all bits: 1100 B = 3 10, (you may do so by the not command), Second, add 1, 1101 B = -3 10, We may achieve this representation in one step using the NEG (negate) command.

15 11.15/36 2’complement (Cont’d) With n bits you can represent numbers [-2 n-1, +2 n-1 -1] -1 is a string of 1s, -2 n-1 is 1 and the rest 0s, -8 10 = 1000 B, -7 10 = 1001 B, … -1 10 = 1111 B, What numbers you deal with In the project? What is their range? What about 40000 + 12000 ? OR -5000 + (-30000) = 70111 60110 50101 40100 30011 20010 10001 00000 1111 -21110 -31101 -41100 -51011 -61010 -71001 -81000

16 11.16/36 Bit, Nibble, Byte, Word & Double Word, Decimal, Binary & Hex, ASCII, Unsigned, Signed numbers & 2’complement, Logic Commands and masking, Shift Commands, String Commands.

17 11.17/36 The NOT Command Function: Invert bits: reg = NOT reg., Flags: Unchanged, ~ (~ X) = X xNot x 01 10

18 11.18/36 The OR Command Function: dst = dst OR src, Flags CF, OF, ZF, SF, PF, AF, x OR 0 = x, x OR 1 = 1, x OR x = x, x OR y = y OR x, (x OR y) OR z = x OR (y OR z), xyx OR y 000 011 101 111

19 11.19/36 The AND Command Function: dst=dst AND src, Flags CF, OF, ZF, SF, PF, AF, x AND 0 = 0, x AND 1 = x, x AND x = x, x AND y = y AND x, (x AND y) AND z = x AND (y AND z), xyx AND y 000 010 100 111

20 11.20/36 The TEST Command Function: Calculate dst AND src, Flags CF, OF, ZF, SF, PF, AF, Source is not destruct, xyx AND y 000 010 100 111

21 11.21/36 The XOR Command Function: dst = dst XOR src, Flags CF, OF, ZF, SF, PF, AF, x XOR 0 = x, x XOR 1 = ~x, x XOR x = 0, x XOR y = y XOR x, (x XOR y) XOR z = x XOR (y XOR z), xyx XOR y 000 011 101 110

22 11.22/36 Masking – Reset Assume we want to reset all bits in BX – excluding bit# 4 & #9, That is we want all other bits to become 0, But bits 4 & 9 to remain untouched. The proper mask is: 0000 0010 0001 0000 (that is 0210H): FEDCBA98 76543210 Bit # 00000010 00010000 The Mask 10011010 01101001 BX AND 10011010 01101001 BX 000000 0 0000000 The Mask

23 11.23/36 Masking – Set Assume we want to set bit# 6 in AX That is we want bit 6 to become 1, and all other bits to remain untouched. The proper mask is: 0000 0000 0100 0000 (that is 0040H): FEDCBA98 76543210 Bit # 00000000 01000000 The Mask 10111010 00101011 AX OR 10111010 00101011 AX 1 The Mask

24 11.24/36 Masking – Invert Assume we want to invert bits # 2 & 12 in DX That is: –If (bit# 2=1) reset it to 0, else set it to 1, –If (bit# 12=1) reset it to 0, else set it to 1, The proper mask is: 0001 0000 0000 0100 (that is 1004H): FEDCBA98 76543210 Bit # 00010000 00000100 The Mask 10011010 00111011 DX XOR 10011010 00111011 DX The Mask 1 0

25 11.25/36 Bit, Nibble, Byte, Word & Double Word, Decimal, Binary & Hex, ASCII, Unsigned, Signed numbers & 2’complement, Logic Commands and masking, Shift Commands, String Commands.

26 11.26/36 The Shift Commands … These are the shift commands: SHL, SHR, SAL, SAR, ROL, ROR, RCL and the RCR command, All work on 8 or 16 bits, All are executed once, or use CL as counter, All move bits one place to the left or one bit to the right, In all of them – the bit in the end (left or right) will go to the carry flag, The question is: from where are we getting the ‘new bit?’

27 11.27/36 The Shift Commands (Cont’d) … RegisterCF SHL: 0 RegisterCF SHR: 0 RegisterCF SAL: 0 RegisterCF SAR:

28 11.28/36 The Shift Commands (Cont’d) ROL: CF Register ROR: CF Register RCL: CF Register CF Register RCR:

29 11.29/36 Bit, Nibble, Byte, Word & Double Word, Decimal, Binary & Hex, ASCII, Unsigned, Signed numbers & 2’complement, Logic Commands and masking, Shift Commands, String Commands.

30 11.30/36 String Commands All String commands can handle byte or word data, These are the 5 pairs string commands: MOVSB & MOVSW, SCASB & SCASW, CMPSB & CMPSW, LODSB & LODSW, STOSB & STOSW, Usually they use: DS:SI as source and ES:DI as destination, All can be used with the prefix REP, REPE and REPNE.

31 11.31/36 The MOVSB & MOVSW Commands Move String do not affect the flags, MOVSB: copy byte: [SI] -> [DI], IF (df=0) than {si = si + 1, di = di + 1} else {si = si - 1, di = di - 1}, MOVSW: copy word: [SI] -> [DI], IF (df=0) than {si = si + 2, di = di + 2} else {si = si - 2, di = di - 2}

32 11.32/36 The Set / Clear DF Commands CLD: Clear Direction Flag, Format: CLD, Flags Affected: DF, Function: Resets DF to 0. Used with string commands, IF DF=0, indices (SI, DI) are incremented, STD: Set Direction Flag, Format: STD, Flags Affected: DF, Function: Sets DF to 1. Used with string commands. IF DF=1, indices (SI, DI) are decremented.

33 11.33/36 The CMPSB & CMPSB Commands Compare the current bytes (or words) of the source and destination strings by subtracting the destination from the source and recording the properties of the result in FLAGS, CMPSB: execute bytes comparison: [SI] - [DI], IF (df=0) than {si = si + 1, di = di + 1} else {si = si - 1, di = di - 1}, CMPSW: execute words comparison: [SI] - [DI], IF (df=0) than {si = si + 2, di = di + 2} else {si = si - 2, di = di - 2}

34 11.34/36 The SCASB & SCASW Commands Compare the current bytes (or words) of AL (or AX) and destination strings by subtracting the destination from the accumulator and recording the properties of the result in FLAGS, SCASB: execute bytes comparison: AL - [DI], IF (df=0) than {di = di + 1} else {di = di - 1}, SCASW: execute words comparison: AX - [DI], IF (df=0) than {di = di + 2} else {di = di - 2}

35 11.35/36 The STOSB and STOSW Commands Store String do not affect the flags, STOSB: copy byte: [AL] -> [DI], IF (df=0) than {di = di + 1} else {di = di - 1}, STOSW: copy word: [AX] -> [DI], IF (df=0) than {di = di + 2} else {di = di - 2}

36 11.36/36 The LODSB and LODSW Commands Load String do not affect the flags, LODSB: copy byte: [SI] -> [AL], IF (df=0) than {si = si + 1} else {si = si - 1}, LODSW: copy word: [SI] -> [AX], IF (df=0) than {si = si + 2} else {si = si - 2}

37 11.37/36 Code Example movah, [bp+4]; ah holds # of rows movdi, [bp+6]; output string address movsi, [bp+8]; input string address cld; clear direction flag for string commands moval, ‘ ‘; will be used in the stosb command xorch, ch; cx will be our columns counter Nxt_R:movcl, [bp+5]; cx holds # of columns Nxt_C:movsb; [di] <- [si], di++, si++ stosb ; [di] <- al, di++ loopNxt_C; end of row! movbyte ptr [di], 0dh; [di] <- CR, incdi movbyte ptr[di], 0ah; [di] <- LF, incdi decah; decrement # of rows jnzNxt_R; end of matrix? movbyte ptr[di], '$‘; Yes! – put the ‘end of string’ sign – ‘$’

38 11.38/36 Summary Bit, Nibble, Byte, Word & Double Word, Decimal, Binary & Hex, ASCII, Unsigned, Signed numbers & 2’complement, Logic Commands and masking, Shift Commands, String Commands.

39 11.39/36 Repeat: From Bits and Pieces Till Strings. The End.


Download ppt "11.1/36 Repeat: From Bits and Pieces Till Strings."

Similar presentations


Ads by Google