You are on page 1of 22

2.

SIGNED AND UNSIGNED DATA PROCESSING AND


FLOW CONTROL THROUGH JUMPS AND LOOPS

2.1 Information representation in computer systems 1


Computer systems store information (data and instructions) using electronic circuits, called flipflops (or bistables), that have two stable states: on/off. The state of a bistable can be used to
represent a bit (i.e. binary digit: 0, 1) or a boolean value (true, false).
Data types with more than two possible values are stored using sequences of bits:
byte (B) a sequence of 8 bits: can store maximum 28 (256) values
word (w) a sequence of 16 bits: can store maximum 216 (65,536) values
double word (dw) a sequence of 32 bits: can store maximum 232 values
Unsigned (positive) integer numbers are represented using the natural binary convention. For
signed (positive and negative) integer numbers there are several representation alternatives
that will be further described and discussed.
Note: the decimal value corresponding to a sequence of bits representing a signed integer
number cannot be computed unless the type of representation is specified.

2.1.1 Sign and magnitude


In the first approach, the problem of representing a number's sign can be to use the most
significant bit (msb) to represent the sign: msb is 0 for a positive number, and 1 for a negative
number. The remaining bits in the number indicate the magnitude (or absolute value). Hence in
a byte, with only 7 bits (apart from the sign bit), the magnitude can range from 0000000 (0) to
1111111 (127). Thus you can represent numbers from 12710 to +12710 once you add the sign
bit (the eighth bit). A consequence of this representation is that there are two ways to represent
zero, 00000000 (+0) and 10000000 (0).
To obtain the sign and magnitude representation of a decimal value, follow this algorithm:
if the number is positive (4310):
o
1

transform the number in natural binary (00101011)

parts of this section were inspired from http://en.wikipedia.org/wiki/Signed_number_representations

if the number is negative (-4310):


o

transform the positive number in natural binary (00101011)

complement the sign bit (10101011)

To obtain the decimal value of a sign and magnitude encoded number, follow this algorithm:
transform the magnitude in decimal,
transform the sign bit into + (if msb=0) or (if msb=1).

2.1.2 One's Complement


In one's complement, positive numbers are represented as usual in natural binary, while
negative numbers are represented as the bitwise complement of the positive value.
In ones complement (exactly as in signed magnitude):
the leftmost bit indicates the sign (1 is negative, 0 is positive),
there are two representations of 0: 00000000 (+0) and 11111111 (0),
the range of numbers that can be represented with a byte is 12710 to +12710.
To obtain the ones complement representation of a decimal value, follow this algorithm:
if the number is positive (4310):
o

transform the number in natural binary (00101011)

if the number is negative (-4310):


o

transform the positive number (4310) in natural binary (00101011)

complement all the bits (11010100)

To obtain the decimal value of a ones complement encoded number, follow this algorithm:
if the sign bit (msb) is 0 (00101011):
o

transform the number in decimal (4310)

if the sign bit (msb) is 1 (11010100):


o

complement all the bits to obtain the natural binary representation (00101011)

transform the number in decimal (4310)

place the sign in front of the number (-4310).

2.1.3 Two's Complement


The problems of multiple representations of 0 and the need for a special algorithm to add
negative numbers represented in signed magnitude or ones complement are circumvented by a
system called two's complement. In two's complement, positive numbers are represented as
usual in natural binary, while negative numbers are represented by the bit pattern which is one
greater (in an unsigned sense) than the ones' complement of the positive value.
In two's complement:
the leftmost bit indicates the sign (1 is negative, 0 is positive),
there is only one zero, represented as 00000000,
the range of numbers that can be represented with a byte is 12810 to +12710.
To obtain the twos complement representation of a decimal value, follow this algorithm:
if the number is positive (4310):
o

transform the number in natural binary (00101011)

if the number is negative (-4310):


o

transform the positive number (4310) in natural binary (00101011)

complement all the bits (11010100)

add 1 (11010101)

To obtain the decimal value of a twos complement encoded number, follow this algorithm:
if the sign bit (msb) is 0 (00101011):
o

transform the number in decimal (4310)

if the sign bit (msb) is 1 (11010101):


o

subtract 1 (11010100)

complement all the bits to obtain the natural binary representation (00101011)

transform the number in decimal (4310)

place the sign in front of the number (-4310).

2.1.4 Comparison table


The following table shows the positive and negative integers that can be represented using 4
bits.

Table 1. 4-bit integer representation

Unsigned
(natural binary)

Sign and
magnitude

Ones
complement

Twos
complement

N/A

N/A

N/A

N/A

1111

N/A

N/A

N/A

1110

N/A

N/A

N/A

1101

N/A

N/A

N/A

1100

N/A

N/A

N/A

1011

N/A

N/A

N/A

1010

N/A

N/A

N/A

1001

N/A

N/A

N/A

1000

N/A

N/A

N/A

+7
+6
+5
+4
+3

0111

0111

0111

0111

0110

0110

0110

0110

0101

0101

0101

0101

0100

0100

0100

0100

0011

0011

0011

0011

+2
+1
+0
0
1
2
3
4
5
6
7
8
9

0010

0010

0010

0010

0001

0001

0001

0001

0000

0000

0000

N/A

1000

1111

N/A

1001

1110

1111

N/A

1010

1101

1110

N/A

1011

1100

1101

N/A

1100

1011

1100

N/A

1101

1010

1011

N/A

1110

1001

1010

N/A

1111

1000

1001

N/A

N/A

N/A

1000

N/A

N/A

N/A

N/A

Decimal
+16
+15
+14
+13
+12
+11
+10
+9
+8

0000

2.2 x86 data processing instructions


Data processing instructions are the instructions responsible for all arithmetic operations (such
as add, subtract, multiply, divide, etc.) and logic operations (such as and, or, exclusive or, shift,
rotate, etc.). Each microprocessor comes with its specific instruction set, which includes
particular types and flavors of data processing instructions. The most important data processing
instructions provided in the x86 instruction set are listed in Table 2.
Table 2. x86 data processing instructions

Instruction

Usage

Description

INC Increment

INC src

Increment src

DEC Decrement

DEC src

Decrement src

ADD Add

ADD dest, src

Add src to dest

ADC Add with Carry

ADC dest, src

Add src and CF to dest

SUB Subtract

SUB dest, src

Subtract src from dest

SBB Subtract with Borrow

SBB dest, src

Subtract src and CF from dest

MUL Multiply

MUL src

Multiply the accumulator with src

DIV Divide

DIV src

Divide the accumulator to src

CMP Compare

CMP src1, src2

Subtract src2 from src1 without modifying


the operands

NOT Complement

NOT src

Complement src

AND Logic AND

AND dest, src

Perform logic AND between src and dest


and store the result in dest

OR Logic OR

OR dest, src

Perform logic OR between src and dest and


store the result in dest

XOR Exclusive OR

XOR dest, src

Perform logic XOR between src and dest


and store the result in dest

SHL Shift Left

SHL src, num

Shift src to the left with num positions

SHR Shift Right

SHR src, num

Shift src to the right with num positions

ROL Rotate Left

ROL src, num

Rotate src to the left with num positions

ROR Rotate Right

ROR src, num

Rotate src to the right with num positions

RCL Rotate Left with Carry

RCL src, num

Rotate CF src to the left with num positions

RCR Rotate Right with Carry

RCR src, num

Rotate CF src to the right with num positions

TEST Compare using AND

TEST src1, src2

Perform logic AND between src1 and src2


without modifying the operands

Details and usage examples regarding some of the data processing instructions were given in
Laboratory 1 and are also provided in Section 2.6.

Most data processing instructions modify some or all the arithmetic flags (CF, ZF, SF, OF, PF, AF),
according to the operation result. CF and ZF refer to operations with unsigned numbers
represented in natural binary, while ZF, SF and OF refer to operations with signed numbers
represented in twos complement.
Note that the microprocessor does not know if the operands or the result are signed or
unsigned and it simply modifies all the flags according to the result. Therefore, provided the
following block of instructions:
mov
add

AL, 80h
AL, 90h

the arithmetic flags will be modified as follows:


CF will be 1, because the unsigned result of the operation: 80h (12810) + 90h (14410) =
110h (27210), is larger than the largest unsigned number representable on 8 bits
(12810).
ZF will be 0, because the result of the instruction is not null.
PF will be 0, because the least significant byte of the result (10h) contains an odd number
of ones.
SF will be 0, because the sign of the 8-bit signed result (10h) is zero (positive).
OF will be 1, because the signed result of the operation: 80h (-12810) + 90h (-11210) =
110h (-24010), is smaller than the smallest signed number representable on 8 bits (12810).
The shift and rotation operations are exemplified in the following figures.

2.3 x86 jump instructions


Jump instructions are flow control instructions (instructions which are used to control where
the program continues). They are exceptions in the regular sequential execution of instructions,
telling the microprocessor to continue executing instructions from a specified address in the
memory (and not from the next instruction).
The x86 architecture provides one unconditional jump instruction (JMP) and several
conditional jump instructions (Jxx), all presented in Table 3.
Table 3. x86 jump instructions

Instruction

Usage

Condition

Description

JMP

JMP dest

N/A

Jump to destination (unconditional)

JA | JNBE

JA label

(CF)=0 AND
(ZF)=0

Jump to label if above | not below or equal

JAE | JNB | JNC

JAE label

(CF)=0

Jump to label if above or equal | not below | not carry

JB | JNAE | JC

JB label

(CF)=1

Jump to label if below | not above or equal | carry

JBE | JNA

JBE label

(CF)=1 OR
(ZF)=1

Jump to label if below or equal | not above

JE | JZ

JE label

(ZF)=1

Jump to label if equal | zero

JG | JNLE

JG label

(SF)=(OF)
AND (ZF)=0

Jump to label if greater | not lower or equal

JGE | JNL

JGE label

(SF)=(OF)

Jump to label if greater or equal | not lower

JL | JNGE

JL label

(SF)!=(OF)

Jump to label if lower | not greater or equal

JLE | JNG

JLE label

(SF)!=(OF)
OR (ZF)=1

Jump to label if lower or equal | not greater

JNE | JNZ

JNE label

(ZF)=0

Jump to label if not equal | not zero

JNO

JNO label

(OF)=0

Jump to label if not overflow

JNP | JPO

JNP label

(PF)=0

Jump to label if not parity | parity odd

JNS

JNS label

(SF)=0

Jump to label if not signed | positive

JO

JO label

(OF)=1

Jump to label if overflow

JP | JPE

JP label

(PF)=1

Jump to label if parity | parity even

JS

JS label

(SF)=1

Jump to label if signed | negative

The unconditional jump instruction always performs the jump at the specified destination. The
destination can be specified as a label, an address stored in a register or an address stored in a
memory location. In the case of conditional jumps, a boolean condition (regarding the flags) is
first verified and, if the condition is fulfilled, the jump to the specified instruction is performed.
Important Note. Some of the conditional jump instructions (the ones using the words above
and below) refer strictly to unsigned numbers (their conditions involve the value of CF). Some
other conditional jump instructions (the ones using the words greater and lower) refer
strictly to signed numbers (their conditions involve the values of SF and OF). The programmer is

responsible for using the correct conditional jump instruction (after an arithmetic or logic
instruction), because only he knows the interpretation of the numbers he uses.

2.3.1 Using conditional jumps to create decision structures


Decision structures to which you are familiar with in high-level programming languages (ifthen-else, switch-case, etc.) can be implemented in the assembly language using conditional and
unconditional jumps. Table 4 lists several examples of assembly language instruction blocks
equivalent to several if-then-else decision structures.
Table 4. Decision structures examples

Pseudo-code

Assembly equivalent

Notes

if (AL > 13h){


BX = 1234h;
}else{
BX = 4321h;
}

sub
jbe
then: mov
jmp
else: mov
endif:

AL, 13h
else
BX, 1234h
endif
BX, 4321h

In this example we consider the numbers as


being unsigned. If the numbers would have
been signed, then JLE would have been used
instead of JBE.

cmp
jne
then: mov
jmp
else1: cmp
jne
mov
jmp
else2: mov
endif:

AL, 0h
else1
BX, 100h
endif
AL, 1h
else2
BX, 200h
endif
BX, 300h

if (AL == 0h){
BX = 100h
}else if (AL == 1h){
BX = 200h
}else{
BX = 300h
}

2.4 x86 loop instructions


Loop instructions are also flow control instructions. They are similar to the conditional jump
instructions, because their execution involves the following: a) a boolean condition (regarding
the flags) is verified and b) if the condition is fulfilled, the jump to the specified instruction is
performed. However, besides the above, all the loop instructions decrement CX, the default
counter of the x86 architecture. The loop instructions are listed in Table 5.

Table 5. x86 loop instructions

Instruction

Usage

Tested
condition

Description

LOOP

LOOP label

(CX) != 0

Decrement CX (without modifying the flags) and


jump to label if CX is not zero

LOOPE |
LOOPZ

LOOPE label

(CX) != 0
AND (ZF)=1

Decrement CX (without modifying the flags) and


jump to label if CX is not zero and ZF is one.

LOOPNE |
LOOPNZ

LOOPNE label

(CX) != 0
AND (ZF)=0

Decrement CX (without modifying the flags) and


jump to label if CX is not zero and ZF is zero.

2.4.1 Using the loop instruction to create repetitive structures


Repetitive structures to which you are familiar with in high-level programming languages (for
loops, while loops, etc.) can be implemented in the assembly language using loop instructions.
Table 6 lists several examples of assembly language instruction blocks equivalent to several
repetitive structures for high-level programming languages.
Table 6. Repetitive structures examples

Pseudo-code
for (int index=9; index>0; index--){
alpha = alpha*2+beta;
}
count = 10h;
result = 15h;
while ((count > 0) && (result != 21h)){
result = result+2;
}

Assembly equivalent
for:

mov
shl
add
loop

mov
mov
while: add
cmp
loopne

CX, 9h
AX, 1
AX, BX
for
CX, 10h
DX, 15h
DX, 2h
DX, 21h
while

2.5 Exercises
2.5.1 Exercise 1
Objective. This exercise presents the various decimal to binary (and vice-versa) conversion
methods for signed integers, using the various signed numbers representation conventions
(signed magnitude, ones complement and twos complement). This exercise also exemplifies
some binary add operations.
Requierment. Transform the numbers +/-5 and +/-12 in binary (using the various signed
numbers representation conventions)and compute the following sums: (5+12), (-5+12), (-12+5) i (12+-12), using the regular adition algorithm for binary numbers. Transform the results
back into decimal.

Solution.
Transform the numbers from decimal to binary.
Table 7. Decimal to binary transformations

Decimal

Sign and magnitude

Ones complement

Twos complement

natural binary: 00000101

natural binary: 00000101

natural binary: 00000101

-5

natural binary: 00000101


flip the sign bit: 10000101

natural binary: 00000101


flip all the bits: 11111010

natural binary: 00000101


flip all the bits: 11111010
add 1: 11111011

+12

natural binary: 00001100

natural binary: 00001100

natural binary: 00001100

-12

natural binary: 00001100


flip the sign bit: 10001100

natural binary: 00001100


flip all the bits: 11110011

natural binary: 00001100


flip all the bits: 11110011
add 1: 11110100

+5

Add the signed magnitude binary numbers using the regular addition algorithm and transform
the results back in decimal.
5+12

-5+12

-12+-5

12+-12

00000101
00001100
________
00010001

10000101
00001100
________
10010001

10001100
10000101
________
00010001

00001100
10001100
________
10011000

17

-17

16

-24

Add the ones complement binary numbers using the regular addition algorithm and
transform the results back in decimal.
5+12

-5+12

-12+-5

12+-12

00000101
00001100
________
00010001

11111010
00001100
________
00000110

11110011
11111010
________
11101101

00001100
11110011
________
11111111

17

-18

Add the twos complement binary numbers using the regular addition algorithm and
transform the results back in decimal.

5+12

-5+12

-12+-5

12+-12

00000101
00001100
________
00010001

11111011
00001100
________
00000111

11110100
11111011
________
11101111

00001100
11110100
________
00000000

17

-17

Note: the regular addition algorithm for binary numbers can be successfully used (issues
correct results) only if the signed decimal numbers are represented in binary twos
complement. This is the reason why the twos complement representation is the most widely
used representation in computer systems.

2.5.2 Exercise 2
Requirement. Transform in decimal the following sequences of bits: 00110011, 10110011,
01010101, 11010101. Consider that the above sequences of bits are represented in binary using
a) signed magnitude representation; b) ones complement representation; c) twos complement
representation.

2.5.3 Exercise 3
Objective. Understand the effect of executing the ADD, ADC, SUB and SBB instructions and the
role of the carry flag (CF).
Requirement. Write a program that adds/subtracts two 16-bit unsigned numbers, initially
stored in CX and DX. The two 16-bit operations should be done in two steps using two 8-bit
operations.
Solution.
1. Start the emulator.
2. Use the Source Code window to write the following program:

org

100h

init:

mov
mov

CX, 6234h
DX, 49D0h

sum:

mov
add
mov
adc

AL, CL
AL, DL
AH, CH
AH, DH

dif:

mov
mov
sub
mov
sbb

AX, 0h
AL, CL
AL, DL
AH, CH
AH, DH

int

20h

3. Understand the program!


3.1. The first line of this program (org 100h) is not an instruction. This is an assembly
directive specifying that the next instruction (and consequently the whole program)
will be loaded in the memory (in the code segment) starting with address 100h.
3.2. The second instruction is preceded by a label (init) that can be used to reference this
instruction from a different place in the program. In this case the label is only used to
make the code easier to understand.
3.3. The block of instructions labeled init initializes the CX and DX registers with the two 16bit numbers.
3.4. The block of instructions labeled sum performs the sum of the two numbers in two
steps: a) adds the least significant bytes in AL and b) adds the most significant bytes and
the carry flag (CF) in AH.
3.5. The block of instructions labeled dif performs the difference of the two numbers in two
steps: a) subtracts the least significant byte of the second number (in DL) from the least
significant byte of the first number (in CL) and stores the result in AL and b) subtracts
the most significant byte of the second number (in DH) and the carry flag (CF) from the
most significant byte of the first number (in CH) and stores the result in AH.
3.6. The instruction int 20h is a software interrupt. It ends the current program and returns
control to the operating system.
4. Save the program (File menu -> Save As submenu) with the name lab2_prog1.asm.
5. Compile the program:
5.1. Click the Compile button to compile the program.
5.2. You will be prompted to save the executable file. Save it with the recommended name
(lab2_prog1.com).

5.3. View the compilation status in the Assembler Status dialog. If the program was edited
correctly the message should be lab2_prog1.com is assembled successfully into 27
bytes.
6. Load the executable program in the emulator.
6.1. Click the Run button to load the program in the emulator and execute it.
7. Execute the program step-by-step, watch the status change of the registers, memory
locations, flags, etc. and write down observations.
7.1. Click the Reload button to reload the executed program.
7.2. Execute all the instructions step-by-step by clicking successively the Single Step button.
8. Write down conclusions regarding the effect of the various instructions on the registers,
flags and memory locations.

2.5.4 Exercise 4
Objective. Understand the effect of executing the AND, OR, NOT and SHL instructions.
Requirement. Write a program that implements the logic function presented below, using as
inputs the following 8-bit numbers: alpha=26h, beta=3Fh, gamma=99h, theta=8Dh.
NOT ((alpha << 8 OR gamma) AND (beta << 8 OR theta))
Note. OR, AND, NOT are the regular logic operators and <<, >> are the shift left and shift right
operators.
Solution.
1. Start the emulator.
2. Use the Source Code window to write the following program:

init:

org

100h

mov
mov

AX, 0h
BX, 0h

part1: mov
shl
or

AL, 26h
AX, 8h
AL, 99h

part2: mov
shl
or

BL, 3Fh
BX, 8h
BL, 8Dh

part3: and
not

AX, BX
AX

int

20h

3. Understand the program!


3.1. The first line of this program (org 100h) is not an instruction. This is an assembly
directive specifying that the next instruction (and consequently the whole program)
will be loaded in the memory (in the code segment) starting with address 100h.
3.2. The block of instructions labeled init initializes the AX and BX registers with zero.
3.3. The block of instructions labeled part1 performs the first part of the logic function (alpha
<< 8 OR gamma) using the register AX. alpha=26h is loaded in AL, then AX is shifted to the
left with 8 positions and finally, the OR operation is performed between AL and
gamma=99h.
3.4. The block of instructions labeled part2 performs the second part of the logic function
(beta << 8 OR theta) using the register BX. beta=3Fh is loaded in BL, then BX is shifted to
the left with 8 positions and finally, the OR operation is performed between BL and
theta=8Dh.
3.5. The block of instructions labeled part3 performs the final part of the logic function: it
performs the AND operation between AX and BX and then complements the result in AX.
3.6. Finally, the instruction int 20h ends the current program and returns control to the
operating system.
4. Continue this exercise by performing the same steps as in the previous exercise.

2.5.5 Exercise 5
Objective. Understand the effect of executing the XOR and SHR instructions.
Requirement. Write a program that implements the logic function presented below, using as
inputs the following 16-bit numbers: alpha=1A26h, beta=553Fh.
(alpha >> 3) XOR (beta >> 5)
Note. XOR, is the regular logic operator and >> is the shift right operator.

2.5.6 Exercise 6
Objective. Understand the effect of executing the ROR, ROL, RCR and RCL instructions and note
the difference between the rotations without carry and the rotations with carry.
Requirement. Write a program that implements the logic functions presented below, using as
inputs the following 8-bit numbers: alpha=26h, beta=3Fh.
(alpha ROR 2) AND (beta ROL 5)
(alpha RCR 2) AND (beta RCL 5)
Note. ROR, ROL, RCR and RCL are the rotate right/left and rotate right/left with carry operators.

2.5.7 Exercise 7
Objective. Understand the effect of executing the CMP and JA instructions. Understand labels
and instruction referencing labels.
Requirement. Write a program that finds the maximum among three unsigned 16-bit numbers
(alpha=1234h, beta=8004h, gamma=072Fh), initially stored in AX, BX and CX.
Solution.
1. Start the emulator.
2. Use the Source Code window to write the following program:
org

100h

mov
mov
mov

AX, 1234h
BX, 8004h
CX, 072Fh

mov

DX, AX

compareBX:

cmp
ja
mov

DX, BX
compareCX
DX, BX

compareCX:

cmp
ja
mov

DX, CX
exit
DX, CX

exit:

int

20h

init:

3. Understand the program!


3.1. The block of instructions labeled init initializes the AX, BX and CX registers with the 16bit numbers.
3.2. Going further, register DX, which will store the maximum is loaded with the value of the
first number (we make the supposition that this is the maximum).
3.3. The instruction cmp DX, BX compares the first two numbers by subtracting the value in
BX from the value in DX. The result is not stored anywhere, but the flags are modified
accordingly. For example, if the unsigned value in BX is larger than the unsigned
number in DX, then CF will be 1.
3.4. The instruction ja compareCX uses the carry flag to take a decision: jump to label
compareCX or continue with the next instruction? We use this instruction (JA) and not
JG because the numbers we are comparing are unsigned. If the unsigned number in BX
was larger than the unsigned number in DX, then the microprocessor continues with
the next instruction, which replaces the maximum (in DX) with the new maximum (in
BX). Otherwise, the microprocessor ignores the value in BX and jumps to label
compareCX.

3.5. The instruction cmp DX, CX compares the maximum with the third number by
subtracting the value in CX from the value in DX. The result is not stored anywhere, but
the flags are modified accordingly. For example, if the unsigned value in CX is larger
than the unsigned number in DX, then CF will be 1.
3.6. The instruction ja exit uses the carry flag to take a decision: jump to label exit or continue
with the next instruction? We use this instruction (JA) and not JG because the numbers
we are comparing are unsigned. If the unsigned number in CX was larger than the
unsigned number in DX, then the microprocessor continues with the next instruction,
which replaces the maximum (in DX) with the new maximum (in CX). Otherwise, the
microprocessor ignores the value in CX and jumps to label exit.
3.7. Finally, the instruction int 20h ends the current program and returns control to the
operating system.
3.8. Decide which of the three unsigned numbers is bigger and which of the two jumps will
be taken (and which will not be taken) before executing the program!
4. Save the program (File menu -> Save As submenu) with the name lab2_ex7.asm.
5. Compile the program:
5.1. Click the Compile button to compile the program.
5.2. You will be prompted to save the executable file. Save it with the recommended name
(lab2_ex7.com).
5.3. View the compilation status in the Assembler Status dialog. If the program was edited
correctly the message should be lab2_ex7.com is assembled successfully into 25 bytes.
5.4. Click the View button -> Symbol Table to view the symbol table associated with this
program. Note that the labels init, compareBX, compareCX and exit are associated with
some offsets (100h, 10Bh, 110h, 117h), representing the memory addresses where the
corresponding instructions are stored.
6. Load the executable program in the emulator.
6.1. Click the Run button to load the program in the emulator and execute it.
7. Execute the program step-by-step, watch the status change of the registers, memory
locations, flags, etc. and write down observations.
7.1. Click the Reload button to reload the executed program.
7.2. Note that the instructions ja compareCX and ja exit are replaced in the emulator by the
instructions jnbe 0111h and jnbe 0117h. Remember (see Table 3) that the instructions
JA and JNBE are equivalent and remember that in the symbol list the labels compareCX
and exit were associated with addresses 110h and 117h.
7.3. Execute all the instructions step-by-step by clicking successively the Single Step button.
8. Write down conclusions regarding the effect of the various instructions on the registers,
flags and memory locations.

2.5.8 Exercise 8
Objective. Understand the difference between processing unsigned numbers and signed
numbers.
Requirement. Modify the previous program to find the maximum among three signed 16-bit
numbers (alpha=1234h, beta=8004h, gamma=072Fh), initially stored in AX, BX and CX.
Indications. Before writing and executing the program decide which of the three signed
numbers is bigger. While executing the program, before any jump instruction, analyze the flags
by clicking the Analyze button in the Flags window.

2.5.9 Exercise 9
Objective. Practice the usage of conditional jump instructions.
Requirement. Modify the program in Exercise 7 to find the minimum among the same three
unsigned 16-bit numbers (alpha=1234h, beta=8004h, gamma=072Fh), initially stored in AX, BX and
CX.

2.5.10 Exercise 10
Objective. Practice the usage of conditional jump instructions.
Requirement. Modify the program in Exercise 8 to find the minimum among the same three
signed 16-bit numbers (alpha=1234h, beta=8004h, gamma=072Fh), initially stored in AX, BX and
CX.

2.6 Appendix 1. Data processing instruction examples


CMP Compare two operands

Example

Usage: CMP src1, src2


Arguments:
src1, src2 8bit or 16bit immediate value, register or memory location;
Effects: Subtracts src2 from src1: (src1) (src2). Flags are set in the same way as the SUB
instruction does, but the result is of the substraction is not saved.
Flags: The CF, ZF, OF, SF, AF, and PF flags are modified acording to the result.
Misc:
usually the next operation would be a conditional jump to perform an operation
according to the result of the comparison;
only one memory argument is allowed and both arguments have to be of the same size.

Example

SBB Integer Subtraction with Borrow

Example

Usage: SBB dest, src


Arguments:
dest 8bit or 16bit register or memory location
src 8bit or 16bit immediate value, register or memory location;
Effects: Subtracts the carry flag and src from dest: (dest) (dest) (src) (CF).
Flags: The CF, ZF, OF, SF, AF, and PF flags are modified acording to the result.
Misc: only one memory argument is allowed and both arguments have to be of the same size.

MUL Unsigned Multiplication of AL or AX

Example

Usage: MUL src


Arguments:
src 8bit or 16bit register or memory location.
Effects:
if src is an 8bit value: multiplies the value stored in AL by src and stores the result in AX:
(AX) (AL) * (src). CF and OF are set to 0 if AH is 0, otherwise they are set to 1.
if src is a 16bit value: multiplies the value stored in AX by src and stores the result in DX
concatenated with AX: (DX) (AX)
(AX) * (src). CF and OF are set to 0 if DX is 0,
otherwise they are set to 1.
Flags: CF and OF are modified as mentioned above. The rest of the flags are undefined.

Example

You might also like