11.1/36 repeat: from bits and pieces till strings
TRANSCRIPT
11.1/36
Repeat:
From Bits and Pieces
Till Strings.
11.2/36Overview
• Bit, Nibble, Byte, Word & Double Word,
• Decimal, Binary & Hex,
• ASCII,
• Unsigned, Signed numbers & 2’complement,
• Logic Commands and masking ,
• Shift Commands ,
• String Commands.
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.
11.4/36Bit, 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 .
11.5/36Bit, Nibble, Byte, Word & Double Word
• Nibble – 0110,
• Byte – 11011010,
• Word – 0100111000111111 ,
• Double Word –11000111000011111001111000100110.
MSB
LSB
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.
11.7/36Decimal, 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.
11.8/36Decimal, Binary & Hex (Cont’d)
Dec Binary Hex
0 0 0
1 1 1
2 10 2
3 11 3
4 100 4
5 101 5
6 110 6
7 111 7
Dec Binary Hex
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
16 1 0000 10
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.
11.10/36ASCII 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/36ASCII Code (Cont’d)32H
46H66H
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.
11.13/36Unsigned, Signed numbers & 2’comp.
• Unsigned numbers:0000B = 010 ,0011B = 310 ,1010B = 1010 ,1110B = 1410 ,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,
11.14/362’complement …
• But than – we got two zeros:0000B = The positive 0, 1000B = The negative 0,
• So we got the 2’complement representation:0011B = 310 ,First, invert all bits: 1100B = 310 ,(you may do so by the not command),Second, add 1, 1101B = -310 ,
• We may achieve this representation in one step using the NEG (negate) command.
11.15/362’complement (Cont’d)
• With n bits you can represent numbers [-2n-1 , +2n-1 -1]-1 is a string of 1s,-2n-1 is 1 and the rest 0s,-810 = 1000B,-710 = 1001B,…-110 = 1111B,
• What numbers you deal with In the project?
• What is their range?
• What about 40000 + 12000 ?
• OR -5000 + (-30000) =
7 0111
6 0110
5 0101
4 0100
3 0011
2 0010
1 0001
0 0000
-1 1111
-2 1110
-3 1101
-4 1100
-5 1011
-6 1010
-7 1001
-8 1000
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.
11.17/36The NOT Command
• Function: Invert bits: reg = NOT reg.,
• Flags: Unchanged ,
• ~ (~ X) = X
x Not x
0 1
1 0
11.18/36The 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) ,
x y x OR y
0 0 0
0 1 1
1 0 1
1 1 1
11.19/36The 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),
x y x AND y
0 0 0
0 1 0
1 0 0
1 1 1
11.20/36The TEST Command
• Function: Calculate dst AND src,
• Flags CF, OF, ZF, SF, PF, AF,
• Source is not destruct ,x y x AND y
0 0 0
0 1 0
1 0 0
1 1 1
11.21/36The 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),
x y x XOR y
0 0 0
0 1 1
1 0 1
1 1 0
11.22/36Masking – 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):
F E D C B A 9 8 7 6 5 4 3 2 1 0Bit #
0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0The Mask
1 0 0 1 1 0 1 0 0 1 1 0 1 0 0 1BX
AND 1 0 0 1 1 0 1 0 0 1 1 0 1 0 0 1BX 0 0 0 0 0 0 0 0 0 0 0 0 0 0The Mask
11.23/36Masking – 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):
F E D C B A 9 8 7 6 5 4 3 2 1 0Bit #
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0The Mask
1 0 1 1 1 0 1 0 0 0 1 0 1 0 1 1AX
OR 1 0 1 1 1 0 1 0 0 0 1 0 1 0 1 1AX 1The Mask
11.24/36Masking – 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):
F E D C B A 9 8 7 6 5 4 3 2 1 0Bit #
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0The Mask
1 0 0 1 1 0 1 0 0 0 1 1 1 0 1 1DX
XOR 1 0 0 1 1 0 1 0 0 0 1 1 1 0 1 1DX The Mask 10
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.
11.26/36The 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?’
11.27/36The Shift Commands (Cont’d) …
RegisterCF
SHL: 0
Register CF
SHR: 0
RegisterCF
SAL: 0
Register CF
SAR:
11.28/36The Shift Commands (Cont’d)
ROL:
CF Register
ROR:
CFRegister
RCL:
CF Register
CFRegister
RCR:
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.
11.30/36String 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.
11.31/36The 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}
11.32/36The 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.
11.33/36The 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}
11.34/36The 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}
11.35/36The 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}
11.36/36The 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}
11.37/36Code Example
mov ah, [bp+4] ; ah holds # of rowsmov di, [bp+6] ; output string addressmov si, [bp+8] ; input string addresscld ; clear direction flag for string commandsmov al, ‘ ‘ ; will be used in the stosb commandxor ch, ch ; cx will be our columns counter
Nxt_R: mov cl, [bp+5] ; cx holds # of columns Nxt_C: movsb ; [di] <- [si], di++, si++
stosb ; [di] <- al, di++loop Nxt_C ; end of row!mov byte ptr [di], 0dh ; [di] <- CR, inc dimov byte ptr[di], 0ah ; [di] <- LF, inc didec ah ; decrement # of rowsjnz Nxt_R ; end of matrix?mov byte ptr[di], '$‘ ; Yes! – put the ‘end of string’ sign –
‘$’
11.38/36Summary
• Bit, Nibble, Byte, Word & Double Word,
• Decimal, Binary & Hex,
• ASCII,
• Unsigned, Signed numbers & 2’complement,
• Logic Commands and masking ,
• Shift Commands ,
• String Commands.
11.39/36
Repeat:
From Bits and Pieces
Till Strings.
The End.