0% found this document useful (0 votes)
17 views56 pages

CE 302 Week 10

The document covers signed number arithmetic operations in assembly language, detailing how signed numbers are represented, including the use of the most significant bit for the sign and the conversion of negative numbers using two's complement. It also discusses overflow issues, the overflow flag, and methods to avoid erroneous results through sign extension. Additionally, it touches on string and table operations, including the use of specific registers and the direction flag for processing consecutive memory locations.

Uploaded by

likohov635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views56 pages

CE 302 Week 10

The document covers signed number arithmetic operations in assembly language, detailing how signed numbers are represented, including the use of the most significant bit for the sign and the conversion of negative numbers using two's complement. It also discusses overflow issues, the overflow flag, and methods to avoid erroneous results through sign extension. Additionally, it touches on string and table operations, including the use of specific registers and the direction flag for processing consecutive memory locations.

Uploaded by

likohov635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

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

u Many applications require signed data, &


computers must be able to accommodate such
numbers.
u The most significant bit (MSB) is set aside for the sign
(+ or -) & the rest of the bits are used for the
magnitude.
u The sign is represented by 0 for positive (+) numbers
and 1 for negative (-) numbers.
SIGNED NUMBER ARITHMETIC OPERATIONS
signed byte operands
u In signed byte operands, D7 (MSB) is the sign and D0 to D6
are set aside for the magnitude of the number.
u If D7 = 0, the operand is positive.
u If D7 = 1, it is negative.
SIGNED NUMBER ARITHMETIC OPERATIONS
positive numbers
u The range of positive numbers that can be represented by the
format above is 0 to +127.
u If a positive number is larger than +127, a word-sized operand must be
used.
SIGNED NUMBER ARITHMETIC OPERATIONS
negative numbers

u For negative numbers D7 is 1, but the magnitude is represented


in 2's complement.

To convert to negative number representation


(2's complement), follow these steps:
1 1. Write the magnitude of the number in 8-bit
binary (no sign).
2. Invert each bit.
3. Add 1 to it.
SIGNED NUMBER ARITHMETIC OPERATIONS
negative numbers

The range of byte-


sized negative
numbers is -1 to -128.
SIGNED NUMBER ARITHMETIC OPERATIONS
negative numbers
Byte-sized signed number ranges.
SIGNED NUMBER ARITHMETIC OPERATIONS
word-sized signed numbers
u A word is 16 bits in length in x86 computers.
u Setting aside the MSB (D15) for the sign leaves a total of 15 bits (D14 – D0)
for the magnitude.
u A range of -32768 to +32767.

• Larger numbers must be treated as a multiword


operand, processed chunk by chunk.
– The same way as unsigned numbers.
SIGNED NUMBER ARITHMETIC OPERATIONS
word-sized signed numbers

The range of signed word operands.


SIGNED NUMBER ARITHMETIC OPERATIONS
word-sized signed numbers

u To convert a negative number to its word operand representation,


the same three steps discussed in negative byte operands are used:

1. Write the magnitude of the number in 8-bit binary (no sign).


2. Invert each bit.
3. Add 1 to it.
SIGNED NUMBER ARITHMETIC OPERATIONS
signed number operations overflow problem

u When using signed numbers, a serious issue, the overflow


problem, must be dealt with.
u The CPU understands only 0s & 1s, ignoring the human convention of
positive/negative numbers.
u The CPU indicates the problem with the OF (overflow) flag.

u If the result of an operation on signed numbers is too large


for the register, an overflow occurs.
SIGNED NUMBER ARITHMETIC OPERATIONS signed
number operations overflow problem

+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.

The CPU designers created the overflow flag


to inform the programmer that the result of
the signed number operation is erroneous.
The results of previous slide

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

u In 8-bit signed number operations, OF is set to 1 if:


u There is a carry from D6 to D7 but no carry out of D7.
u (CF = 0)
u There is a carry from D7 out, but no carry from D6 to D7.
u (CF = 1)

u In other words, the overflow flag is set to 1 if there is a carry


from D6 to D7 or from D7 out, but not both.
u If there is a carry both from D6 to D7, and from D7 out,
then OF = 0.
SIGNED NUMBER ARITHMETIC OPERATIONS
the overflow flag in 8-bit operations
u In Example 6-4, since there is only a carry from
D6 to D7 and no carry from D7 out, OF = 1.
u Examples 6-5, 6-6, and 6-7 give further illustrations of the overflow flag in
signed arithmetic.
SIGNED NUMBER ARITHMETIC OPERATIONS the
overflow flag in 8-bit operations
SIGNED NUMBER ARITHMETIC OPERATIONS
overflow flag in 16-bit operations

u In a 16-bit operation, OF is set to 1 in two cases:


u A carry from D14 to D15, but no carry out of D15.
u (CF = 0).
u A carry from D15 out but no carry from D14 to D15.
u (CF = 1)

u Again, the overflow flag is low (not set) if there is a carry


from both D14 to D15 and from D15 out.
SIGNED NUMBER ARITHMETIC OPERATIONS
overflow flag in 16-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

u Sign-extending the operand can avoid problems associated with


signed number operations.
u This copies the sign bit (D7) of the lower byte of a register into the upper
bits of the register.
u Or copies the sign bit of a 16-bit register into another register.

u Two directives are used to perform sign extension:


u CBW (convert signed byte to signed word)
u CWD (convert signed word to signed double word)
SIGNED NUMBER ARITHMETIC OPERATIONS
avoiding erroneous results
u CBW will copy D7 (the sign flag) to all bits of AH.
u The operand is assumed to be AL, and the previous contents of AH are destroyed.

OR:
SIGNED NUMBER ARITHMETIC OPERATIONS
avoiding erroneous results

u CWD sign-extends AX, copying D15 of AX to all bits of the DX register.


u This is used for signed word operands.

The sign of AX is copied


to the DX register.

Another example :
SIGNED NUMBER ARITHMETIC OPERATIONS
avoiding erroneous results

Example 6-10 shows 6-4


rewritten to correct for
overflow.

If the overflow flag is not


raised, the result of the
signed number is correct
& JNO (jump if no
overflow) will jump to
OVER.
SIGNED NUMBER ARITHMETIC OPERATIONS
avoiding erroneous results

Example 6-10 shows 6-4


rewritten to correct for
overflow.

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:

– If the possibility of overflow exists, byte-sized signed


numbers should be sign-extended into a word,
– Word-sized signed operands should be sign-extended
before they are processed
Example

data segment mov bl,-12h


veri1 db -23h cbw
veri dw -13h add al,bl
ends
code segment mov ax, veri
; set segment registers: cwd
mov ax, data mov bx,-0FEh
mov ds, ax cwd
mov es, ax add ax,bx
mov al, veri1
mov ax, 4c00h ; exit to operating system.
cbw
int 21h
ends
In summary

u OF flag just checks the carry from D6 to D7 OR from D7


out.
u If there is a carry, then OF flag is set
u It says that the result will not fit into 7 bits; 8th bit is also
used.
u The sign is then shown in AH (after CBW instruction)
u The same idea can be extended to 16-bit operations…
SIGNED NUMBER ARITHMETIC OPERATIONS
IDIV signed number division

u IDIV means "integer division“, used for signed number division.


u All 8088/86 arithmetic instructions are for integer numbers, regardless
of whether the operands are signed or unsigned.
u Real numbers operations are done by the 8087 coprocessor.
SIGNED NUMBER ARITHMETIC OPERATIONS
IMUL signed number multiplication
u Similar in operation to the unsigned multiplication.
u Operands in signed number operations can be positive or negative, and the
result must indicate the sign.
SIGNED NUMBER ARITHMETIC OPERATIONS
IMUL signed number multiplication
Program 6-1 computes the average of temperatures: +13, -10, +19, +14, -18, -9, +12, -19, +16, Celsius.

Each data byte was sign-


extended and added to
BX, computing the sum,
which is a signed word.
The sum & count were
sign-extended, and by
dividing the total sum by
the count (9), the
average was calculated.

See the entire program listing on page 182 of your textbook.


SIGNED NUMBER ARITHMETIC OPERATIONS
Example

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.

Why do we need it?


SIGNED NUMBER ARITHMETIC OPERATIONS
SAR/SAL

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.

u In unsigned number comparisons CF and ZF are checked for


conditions of larger, equal, and smaller.
u In signed number comparison, OF, ZF, SF are checked.
SIGNED NUMBER ARITHMETIC OPERATIONS
CMP signed number comparison

Mnemonics used to detect the conditions above:

Dest>src
Dest>=src
Dest<src
Dest<=src
Dest=src

– Example 6-12 on page 185 should help clarify how the


condition flags are affected by the compare instruction.
– Program 6-2 on page 186 is an example of the application of
the signed number comparison.
• It uses the data in Program 6-1 and finds the lowest temperature.
Example
CMP dest, source

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

u String instructions in the x86 family are capable of operations


on a series of operands located in consecutive memory
locations.
u While CMP can compare only 2 bytes (or words) of data, CMPS
(compare string) compares two arrays of data located in
memory locations.
u Pointed at by the SI and DI registers.
STRING AND TABLE OPERATIONS
SI & DI, DS & ES in string instructions
u For string operations to work, CPU designers set aside certain
registers for specific functions, to permanently provide
source/destination operands.
u In 8088/86 processors, SI & DI registers always point to the source and
destination operands, respectively.
u To generate the physical address, 8088/86 always uses SI as the
offset of the DS (data segment) register. and DI as the offset of
ES (extra segment).
u The ES register must be initialized for the string operation to work.
STRING AND TABLE OPERATIONS
byte/word operands in string instructions
The operand can be a byte or a word, distinguished by letters B (byte) & W (word) in
the mnemonic.

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).

• The programmer specifies the choice of increment or decrement


by setting the direction flag high or low.
– CLD (clear direction flag) will reset (zeroes) DF, telling the string instruction
to increment the pointers automatically.
• Sometimes is referred to as autoincrement.
– STD (set the direction flag) performs the opposite function.
• Sets DF to 1, indicating that the pointers SI and DI should be
decremented automatically.
STRING AND TABLE OPERATIONS
REP prefix
u The REP (repeat) prefix allows a string instruction to perform the
operation repeatedly.
u REP assumes that CX holds the number of times the instruction should be
repeated (until CX becomes zero).
u In Example 6-13 (next slide), after transfer of every byte by the
MOVSB instruction, both the SI and DI registers are incremented
automatically once only (notice CLD).
u The REP prefix causes the CX counter to decrement, and MOVSB is repeated
until CX becomes zero.
u Both DS and ES are set to the same value.
STRING AND TABLE OPERATIONS
REP prefix
Example

.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:

– MOVSW will transfer a word (2 bytes) at a time and increment SI &


DI registers each twice.
• REP will repeat that process until CX becomes zero.
• CX has the value of 10 in it, as 10 words is equal to 20 bytes.
STRING AND TABLE OPERATIONS
STOS instruction
u STOSB stores the byte in register AL into memory locations
pointed at by ES:DI.
u If DF = 0, then DI is incremented once.
u If DF = 1, then DI is decremented.
u STOSW stores the contents of AX in ES:DI and ES:DI+1 (AL into
ES:DI and AH into ES:DI+1)
u If DF = 0, then DI is incremented twice.
u If DF = 1, then DI is decremented twice
STRING AND TABLE OPERATIONS
LODS instructions
u LODSB loads the contents of memory locations pointed at by DS:SI into
AL.
u Increments SI once, if DF = 0.
u Decrements SI once, if DF = 1.
u LODSW loads the contents of memory locations pointed at by DS:SI into
AL, and DS:SI+1 into AH.
u SI is incremented twice if DF = 0, else it is decremented twice.
u LODS is never used with a REP prefix.
STRING AND TABLE OPERATIONS
testing memory using STOSB and LODSB
u Example 6-14 on page 189 (next slide) uses string instructions
STOSB & LODSB to test an area of RAM memory.
u First AAH is written into 100 locations by using word-sized operand AAAAH,
and a count of 50.
u In the test, LODSB brings the contents of memory locations into AL, one by
one, and each is eXclusive-ORed with AAH. (register AH has hex value AA).
u If they are the same, ZF = 1 and the process is continued.
u Otherwise, the pattern written there by the previous routine is not there and
the program will exit.
STRING AND TABLE OPERATIONS
testing memory using STOSB and LODSB

See the entire program listing on page 189 of your textbook.


.DATA mov cl,12
DATA1 DB 'abcdefghijkl'
std
data2 db 12 dup(?)
mov di,11d
;-------------initializing-------------
.CODE mov si,11d
MAIN PROC FAR dongu: lodsb
Example mov ax,@data stosb
mov ds,ax
dec cl
add ax,100
jnz dongu
mov es,ax
cld
MOV AH, 4CH
mov si,offset data1
INT 21H
mov di,offset data2
MAIN ENDP
mov cx,12 END MAIN
rep movsb
STRING AND TABLE OPERATIONS
REPZ and REPNZ prefixes

u Used with CMPSB (W) & SCAS for testing purposes.


u REPZ (repeat zero) - the same as REPE (repeat equal), will repeat the string
operation as long as the source and destination operands are equal (ZF = 1) or
until CX becomes zero.
u REPNZ (repeat not zero) - the same as REPNE (repeat not equal), will repeat
the string operation as long as the source and destination operands are not
equal (ZF = 0) or until CX becomes zero.
u CMPS (compare string) - allows the comparison of two arrays of data pointed
at by registers SI & DI.
u CMPS can test inequality of two arrays using "REPNE CMPSB".
Example

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:

– To access the square of any number from 0 to 9, by use of


XLAT, register BX must have the offset address of the look-up
table, and the number whose square is sought must be in the
register AL.
• After XLAT execution, AL will have the square of the number.
STRING AND TABLE OPERATIONS
XLAT instruction and look-up tables
u To get the square of 5 from the table:

– After execution AL will have 25 (19H), the square of 5.

• XLAT is one instruction, equivalent to the following:


mov bx,offset data1
mov al,4
xlat
STRING AND TABLE OPERATIONS
code conversion using XLAT
u XLAT can translate the hex keys of non-ASCII keyboards to ASCII.
u Assuming keys are 0–F, the following is the program to convert the hex
digits of 0–F to their ASCII equivalents.

You might also like