CE 302 Week 10
CE 302 Week 10
Assembly Language
Signed Numbers and Strings
These lecture notes are based on the book by Muhammed Ali Mazidi,
Janice Gillispie Mazidi, Danny Causey; «The x86 PC assembly language,
design, ad interfacing», 5the Ed., Prentice Hall
SIGNED NUMBER ARITHMETIC OPERATIONS
Concept of signed numbers in computers
+96 is added to +70 and the result according to the CPU is -90.
Why? The result was more than AL could handle, because like all other 8-bit
registers, AL could only contain up to +127.
u 96 + 70 = 166 -> 60H + 46H = A6H = 1010 0110 B. (166 in unsigned arithmetic)
u In signed arithmetic first bit is sign bit, therefore, the number is negative so the
value is 2’complement of the result
u So the magnitude is 010 0110 B, 2’s complement of which is 101 1010 = 90.
(64 x 1 + 16x1 + 8x1 + 2x1 =90)
u Because of the sign bit, the result is -90
u Actual result should be +166.
u Bu 7 bits is not enough to accommodate +166 when signed arithmetic is used.
SIGNED NUMBER ARITHMETIC OPERATIONS the
overflow flag in 8-bit operations
OF is set to 1 only
when there is carry
from D14 to D15
or from D15 out,
but not from both.
SIGNED NUMBER ARITHMETIC OPERATIONS
avoiding erroneous results
OR:
SIGNED NUMBER ARITHMETIC OPERATIONS
avoiding erroneous results
Another example :
SIGNED NUMBER ARITHMETIC OPERATIONS
avoiding erroneous results
If OF = 1, result is erroneous
& each operand must be
sign-extended, then added.
This works for addition of
any two signed bytes.
SIGNED NUMBER ARITHMETIC OPERATIONS
avoiding erroneous results
u Analysis of the values in Example 6-10.
u Each is sign-extended and added as follows:
Program 6-1 computes the average of temperatures: +13, -10, +19, +14, -18, -9, +12, -19, +16,
Celsius.
;-------loop---------------------------
BACK: MOV AL, [SI]
.DATA
CBW
SIGN_DAT DB 13, -10, 19, 14, -18, -9, 12, -19, 16
ADD BX, AX
; ORG 0010H
INC SI
AVERAGE DW ?
LOOP BACK
REMAINDER DW ?
;-------------initializing-------------
MOV CL, 9
.CODE
MOV AX, BX
MAINPROC FAR
CWD
MOV AX, @DATA
MOV DS, AX
IDIV CX
MOV CX, 9
MOV AVERAGE, AX
MOV SI, OFFSET SIGN_DATA
MOV REMAINDER, DX
MOV BX, 00
;
MOV AH, 4CH
;
INT 21H
MAINENDP
END MAIN
SIGNED NUMBER ARITHMETIC OPERATIONS
SAR/SAL
u SAR destination, count
u As the bits of the destination are shifted to the right
into CF, the empty bits are filled with the sign bit.
u SAL (shift arithmetic left) and SHL (shift left) do exactly the same
thing.
u Basically, the same instruction with two mnemonics.
u As far as signed numbers are concerned, there is no need for SAL.
SIGNED NUMBER ARITHMETIC OPERATIONS
CMP signed number comparison
u CMP dest, source
u The same for both signed and unsigned numbers.
u The J condition instruction used to make a decision is different from that used
for the unsigned numbers.
Dest>src
Dest>=src
Dest<src
Dest<=src
Dest=src
SF OF ZF
mov al, -5
cmp al,-9 0 0 0 Dest>src
cmp al,-2 1 0 0 Dest<src
cmp al,-5 0 0 1 Dest=src
cmp al,7 1 0 0 Dest<src
Dest>src
OR ZF=1 Dest>=src
Dest<src
Dest<=src
Dest=src
STRING AND TABLE OPERATIONS
word
STRING AND TABLE OPERATIONS
DF, the direction flag
u To process operands in consecutive locations requires the pointer
to be incremented/decremented.
u In string operations, this is achieved by the direction flag.
u Flag register bit 11 (D10) is set aside for the direction flag (DF).
.DATA
mov si,offset data1
DATA1 DB 'abcdefghijkl'
mov di,offset data2
data2 db 12 dup(?)
mov cx,12
;-------------initializing-------------
rep movsb
.CODE
MOV AH, 4CH
MAIN PROC FAR
mov ax,@data INT 21H
mov ds,ax MAIN ENDP
mov es,ax END MAIN
cld
STRING AND TABLE OPERATIONS
REP prefix
u An alternative solution would change only two lines of
code:
u The code below compares the string bytes in the data and extra segments one
byte at a time
cld
mov di,0d
mov si,0d
mov cl,12d
repe cmpsb
u If there is a descrepancy between the data compared, then the loop will
terminate
Example
STRING AND TABLE OPERATIONS
SCASB (scan string)
u SCASB compares each byte of the array pointed at by ES:DI with the contents of
the AL register.
u Depending on which prefix, REPE or REPNE, is used, a decision is made for equality
or inequality.
u In Example 6-16, on page 191, the letter "G" is compared with "M".
u Since they are not equal, DI increments, CX decrements
u Scanning is repeated until letter "G" is found or the CX register is zero.
u SCASB can search for a character in an array & if found, it will be replaced with
the desired character.
Scan the strings
STRING AND TABLE OPERATIONS
XLAT instruction and look-up tables
u A table is commonly referred to as a look-up table.
u To access elements of a table, 8088/86 processors provide the XLAT (translate)
instruction.
u Assume a need for a table for the values of x2, where x is between 0 and 9.
u First the table is generated and stored in memory: