Professional Documents
Culture Documents
S U D P F C J L: 2.1 Information Representation in Computer Systems
S U D P F C J L: 2.1 Information Representation in Computer Systems
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).
To obtain the decimal value of a ones complement encoded number, follow this algorithm:
if the sign bit (msb) is 0 (00101011):
o
complement all the bits to obtain the natural binary representation (00101011)
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
subtract 1 (11010100)
complement all the bits to obtain the natural binary representation (00101011)
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
Instruction
Usage
Description
INC Increment
INC src
Increment src
DEC Decrement
DEC src
Decrement src
ADD Add
SUB Subtract
MUL Multiply
MUL src
DIV Divide
DIV src
CMP Compare
NOT Complement
NOT src
Complement src
OR Logic OR
OR dest, src
XOR Exclusive OR
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
Instruction
Usage
Condition
Description
JMP
JMP dest
N/A
JA | JNBE
JA label
(CF)=0 AND
(ZF)=0
JAE label
(CF)=0
JB | JNAE | JC
JB label
(CF)=1
JBE | JNA
JBE label
(CF)=1 OR
(ZF)=1
JE | JZ
JE label
(ZF)=1
JG | JNLE
JG label
(SF)=(OF)
AND (ZF)=0
JGE | JNL
JGE label
(SF)=(OF)
JL | JNGE
JL label
(SF)!=(OF)
JLE | JNG
JLE label
(SF)!=(OF)
OR (ZF)=1
JNE | JNZ
JNE label
(ZF)=0
JNO
JNO label
(OF)=0
JNP | JPO
JNP label
(PF)=0
JNS
JNS label
(SF)=0
JO
JO label
(OF)=1
JP | JPE
JP label
(PF)=1
JS
JS label
(SF)=1
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.
Pseudo-code
Assembly equivalent
Notes
sub
jbe
then: mov
jmp
else: mov
endif:
AL, 13h
else
BX, 1234h
endif
BX, 4321h
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
}
Instruction
Usage
Tested
condition
Description
LOOP
LOOP label
(CX) != 0
LOOPE |
LOOPZ
LOOPE label
(CX) != 0
AND (ZF)=1
LOOPNE |
LOOPNZ
LOOPNE label
(CX) != 0
AND (ZF)=0
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
Ones complement
Twos complement
-5
+12
-12
+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
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
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.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.
Example
Example
Example
Example
Example