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
SIGNED AND UNSIGNED DATA PROCESSING AND FLOW CONTROL THROUGH JUMPS AND LOOPS
if the number is negative (-4310): o 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).
if the number is negative (-4310): o 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 o 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).
if the number is negative (-4310): o o 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 o o 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).
Ones complement
N/A N/A N/A N/A N/A N/A N/A N/A N/A 0111 0110 0101 0100 0011 0010 0001 0000 1111 1110 1101 1100 1011 1010 1001 1000 N/A N/A
Twos complement
N/A N/A N/A N/A N/A N/A N/A N/A N/A 0111 0110 0101 0100 0011 0010 0001 0000 1111 1110 1101 1100 1011 1010 1001 1000 N/A
Instruction
INC Increment DEC Decrement ADD Add ADC Add with Carry SUB Subtract SBB Subtract with Borrow MUL Multiply DIV Divide CMP Compare NOT Complement AND Logic AND OR Logic OR XOR Exclusive OR SHL Shift Left SHR Shift Right ROL Rotate Left ROR Rotate Right RCL Rotate Left with Carry RCR Rotate Right with Carry TEST Compare using AND
Usage
INC src DEC src ADD dest, src ADC dest, src SUB dest, src SBB dest, src MUL src DIV src CMP src1, src2 NOT src AND dest, src OR dest, src XOR dest, src SHL src, num SHR src, num ROL src, num ROR src, num RCL src, num RCR src, num TEST src1, src2
Description
Increment src Decrement src Add src to dest Add src and CF to dest Subtract src from dest Subtract src and CF from dest Multiply the accumulator with src Divide the accumulator to src Subtract src2 from src1 without modifying the operands Complement src Perform logic AND between src and dest and store the result in dest Perform logic OR between src and dest and store the result in dest Perform logic XOR between src and dest and store the result in dest Shift src to the left with num positions Shift src to the right with num positions Rotate src to the left with num positions Rotate src to the right with num positions
Rotate CF src to the left with num positions Rotate CF src to the right with num positions
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 result (110h) contains an even 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).
Instruction
JMP JA | JNBE JAE | JNB | JNC JB | JNAE | JC JBE | JNA
Usage
JMP dest JA label JAE label JB label JBE label
Condition
N/A (CF)=0 AND (ZF)=0 (CF)=0 (CF)=1 (CF)=1 OR
Description
Jump to destination (unconditional) Jump to label if above | not below or equal Jump to label if above or equal | not below | not carry Jump to label if below | not above or equal | carry Jump to label if below or equal | not above
(ZF)=1 JE | JZ JG | JNLE JGE | JNL JL | JNGE JLE | JNG JNE | JNZ JNO JNP | JPO JNS JO JP | JPE JS JE label JG label JGE label JL label JLE label JNE label JNO label JNP label JNS label JO label JP label JS label (ZF)=1 (SF)=(OF) AND (ZF)=0 (SF)=(OF) (SF)!=(OF) (SF)!=(OF) OR (ZF)=1 (ZF)=0 (OF)=0 (PF)=0 (SF)=0 (OF)=1 (PF)=1 (SF)=1 Jump to label if equal | zero Jump to label if greater | not lower or equal Jump to label if greater or equal | not lower Jump to label if lower | not greater or equal Jump to label if lower or equal | not greater Jump to label if not equal | not zero Jump to label if not overflow Jump to label if not parity | parity odd Jump to label if not signed | positive Jump to label if overflow Jump to label if parity | parity even 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.
Pseudo-code
if (AL > 13h){ BX = 1234h; }else{ BX = 4321h; } if (AL == 0h){ BX = 100h
Assembly equivalent
sub jbe then: mov jmp else: mov endif: cmp jne AL, 13h else BX, 1234h endif BX, 4321h AL, 0h else1
Notes
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.
then:
mov jmp else1: cmp jne mov jmp else2: mov endif:
BX, 100h endif AL, 1h else2 BX, 200h endif BX, 300h
Instruction
LOOP LOOPE | LOOPZ LOOPNE | LOOPNZ
Usage
LOOP label LOOPE label LOOPNE label
Tested condition
(CX) != 0 (CX) != 0 AND (ZF)=1 (CX) != 0 AND (ZF)=0
Description
Decrement CX (without modifying the flags) and jump to label if CX is not zero Decrement CX (without modifying the flags) and jump to label if CX is not zero and ZF is one. Decrement CX (without modifying the flags) and jump to label if CX is not zero and ZF is zero.
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 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
natural binary: 00000101
Twos complement
natural binary: 00000101
+5 -5
natural binary: 00000101 flip the sign bit: 10000101 natural binary: 00001100 natural binary: 00001100 flip the sign bit: 10001100 natural binary: 00000101 flip all the bits: 11111010 natural binary: 00001100 natural binary: 00001100 flip all the bits: 11110011 natural binary: 00000101 flip all the bits: 11111010 add 1: 11111011 natural binary: 00001100 natural binary: 00001100 flip all the bits: 11110011 add 1: 11110100
+12 -12
Add the signed magnitude binary numbers using the regular addition algorithm and transform the results back in decimal. 5+12 00000101 00001100 ________ 00010001 17 -5+12 10000101 00001100 ________ 10010001 -17 -12+-5 10001100 10000101 ________ 00010001 16 12+-12 00001100 10001100 ________ 10011000 -24
Add the ones complement binary numbers using the regular addition algorithm and transform the results back in decimal. 5+12 00000101 00001100 ________ 00010001 17 -5+12 11111010 00001100 ________ 00000110 6 -12+-5 11110011 11111010 ________ 11101101 -18 12+-12 00001100 11110011 ________ 11111111 0
Add the twos complement binary numbers using the regular addition algorithm and transform the results back in decimal. 5+12 00000101 00001100 ________ 00010001 17 -5+12 11111011 00001100 ________ 00000111 7 -12+-5 11110100 11111011 ________ 11101111 -17 12+-12 00001100 11110100 ________ 00000000 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 bi nary 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 init: mov mov mov add mov adc mov mov sub mov sbb int 3. Understand the program!
100h CX, 6234h DX, 49D0h AL, CL AL, DL AH, CH AH, DH AX, 0h AL, CL AL, DL AH, CH AH, DH 20h
sum:
dif:
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: org init: mov mov 100h AX, 0h BX, 0h AL, 26h AX, 8h AL, 99h BL, 3Fh BX, 8h BL, 8Dh AX, BX AX 20h
part1: mov shl or part2: mov shl or part3: and not int
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 init: mov mov mov mov compareBX: cmp ja mov cmp ja mov int 100h AX, 1234h BX, 8004h CX, 072Fh DX, AX DX, BX compareCX DX, BX DX, CX exit DX, CX 20h
compareCX:
exit:
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.
Example
Example
Example
Example
Example