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
Data types with more than two possible values are stored using sequences of bits:
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.
To obtain the sign and magnitude representation of a decimal value, follow this algorithm:
To obtain the decimal value of a sign and magnitude encoded number, follow this algorithm:
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:
To obtain the decimal value of a ones complement encoded number, follow this algorithm:
o complement all the bits to obtain the natural binary representation (00101011)
In two's complement:
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:
o add 1 (11010101)
To obtain the decimal value of a twos complement encoded number, follow this algorithm:
o subtract 1 (11010100)
o complement all the bits to obtain the natural binary representation (00101011)
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:
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).
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.
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.
Tested
Instruction Usage Description
condition
Decrement CX (without modifying the flags) and
LOOP LOOP label (CX) != 0
jump to label if CX is not zero
LOOPE | (CX) != 0 Decrement CX (without modifying the flags) and
LOOPE label
LOOPZ AND (ZF)=1 jump to label if CX is not zero and ZF is one.
LOOPNE | (CX) != 0 Decrement CX (without modifying the flags) and
LOOPNE label
LOOPNZ AND (ZF)=0 jump to label if CX is not zero and ZF is zero.
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.
Add the signed magnitude binary numbers using the regular addition algorithm and transform
the results back in decimal.
17 -17 16 -24
Add the ones complement binary numbers using the regular addition algorithm and
transform the results back in decimal.
17 6 -18 0
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
17 7 -17 0
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.
int 20h
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 16-
bit 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.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.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.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.
Note. OR, AND, NOT are the regular logic operators and <<, >> are the shift left and shift right
operators.
Solution.
org 100h
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.
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.
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.
org 100h
mov DX, AX
3.1. The block of instructions labeled init initializes the AX, BX and CX registers with the 16-
bit 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.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.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.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
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
Example
SBB Integer Subtraction with Borrow
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.
Example
MUL Unsigned Multiplication of AL or AX
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
Example