Вы находитесь на странице: 1из 39

Computer Organization (Lecture 1)

Sunday, November 09, 2014

10:59 PM

Instructor: Rodelyn M. Aguinaldo


*Announced quiz, normally every Friday.
*Edmodo Account
*Blue pen during Lab activities.

Convert 18.6875D to binary


Integral Part = 18D
18/2 => quotient=9 remainder=0
9/2 => quotient=4 remainder=1
4/2 => quotient=2 remainder=0
2/2 => quotient=1 remainder=0
1/2 => quotient=0 remainder=1 (quotient=0 stop)
Hence, 18D = 10010B
Fractional Part = .6875D
.6875*2=1.375 => whole number is 1
.375*2=0.75
=> whole number is 0
.75*2=1.5
=> whole number is 1
.5*2=1.0
=> whole number is 1
Hence .6875D = .1011B
Therefore, 18.6875D = 10010.1011B
From <http://www.ntu.edu.sg/home/ehchua/programming/java/datarepresentation.html>

Convert 18.6875D to hexadecimal


Integral Part = 18D
18/16 => quotient=1 remainder=2
1/16 => quotient=0 remainder=1 (quotient=0 stop)
Hence, 18D = 12H
Fractional Part = .6875D
.6875*16=11.0 => whole number is 11D (BH)
Hence .6875D = .BH
Therefore, 18.6875D = 12.BH
From <http://www.ntu.edu.sg/home/ehchua/programming/java/datarepresentation.html>

1.9 Exercises (Number Systems Conversion)


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

Convert the following decimal numbers into binary and hexadecimal numbers:
108
4848
9000
Convert the following binary numbers into hexadecimal and decimal numbers:
1000011000
10000000
101010101010
Convert the following hexadecimal numbers into binary and decimal numbers:
ABCDE
CPE 005B Page 1

10.
11.
12.
13.
14.
15.

16.
17.
18.
19.

ABCDE
1234
80F
Convert the following decimal numbers into binary equivalent:
19.25D
123.456D
Answers: You could use the Windows' Calculator (calc.exe) to carry out number system
conversion, by setting it to the scientific mode. (Run "calc" Select "View" menu Choose
"Programmer" or "Scientific" mode.)
1101100B, 1001011110000B, 10001100101000B, 6CH, 12F0H, 2328H.
218H, 80H, AAAH, 536D, 128D, 2730D.
10101011110011011110B, 1001000110100B, 100000001111B, 703710D, 4660D, 2063D.
??
From <http://www.ntu.edu.sg/home/ehchua/programming/java/datarepresentation.html>

CPE 005B Page 2

CPU Registers
Sunday, November 30, 2014

9:45 PM

Registers: small areas that can store data temporarily


* the areas are so small that it cannot store files, but the address of these files while the program is running.

16 bit registers (e.g. 8086, 8088, 80286) have 14 16-bit registers


1. Data Register: AX,BX,CX,DX.
a. Ax register - accumulator used to hold the first operand.
i. Ax contains the results of operations like 9+2.
ii. Divided into dual 8 bits (AH 15-8, AL 7-0)
iii. Example: mov ah
b. Bx register - base addresses (bh, bl)
c. Cx register - counter register (ch, cl)
d. Dx register - data register
i. Example: mov dl
2. Segment Addresses: ES,DS,SS,CS
3. Index Addressing: SP,BP,SI,DI
4. Instruction Pointer: IP
5. Flag Register: Flag
mov dl, 41 means:
Dh = 00
Dl = 41
Dx = 0041

32 bit registers: 80386


*The architecture (16bit or 32 bit) determines the width of the data lines. The wider the faster!
Three Categories of Registers:
1. General Purpose Registers
a. For arithmetic and data movement
2. Segment Registers
3. Other Registers
General Purpose Registers: Used for data movement, arithmetic and logic operations.

Addition:
ADD destination, source
ADD destination, immediate data
ADC, which means to add the two operands plus the carry flag.
Subtraction:
SUB destination, source
SUB destination, immediate data
SBB which means to subtract minus the carry flag
*Carry Flag is set to 1 if a larger number was subtracted from a smaller number and a borrow has occurred
CPE 005B Page 3

*Carry Flag is set to 1 if a larger number was subtracted from a smaller number and a borrow has occurred

Multiplication and Division


MUL / DIV = unsigned
IMUL / IDIV = signed
Default Operands for Multiplication

No. of Bits

Multiplicand

Multiplier

Product

8 bits x 8

AL

register or memory AX (16 bits)

16 bits x 16

AX

register or memory DX:AX (32 bits)

32 bits x 32

EAX

register or memory EDX:EAX (64 bits)

Default Operands for Division


No. of BIts

Dividend

Divisor

16 bits / 8

AX

register, memory (8-bit)

32 bits / 16

DX:AX

64 bits / 32

Quotient Remainder
AL

AH

register, memory (16-bit) AX

DX

EDX:EAX register, memory (32-bit) EAX

EDX

*Note: Remainder sign will follow the dividend sign.


Flags
- are register consists of individual binary bits that control the operation of the CPU or reflect the result of arithmetic and logical instructions.
- set when equals to 1 or reset / clear when it equals 0.

Audio
Recording
1. Carry - No Carry NC (0) or Carry CY (1). Kung may carry!
*sets when there is a carry on the Most Significant Digit
*Set if the result of the instruction has gone beyond the limit of the data size (ie. a "carry" has occurred).
*In addition, 255 +1 sets the carry flag, since 256 is beyond the upper limit of 255.
*1111 + 0001 = 0000 (carry flag is turned on)
*In subtraction, 02 - 04 also sets the carry flag since -2 is beyond the lower limit of 0.
*0000 - 0001 = 1111 (carry flag is turned on)
2. Sign - Sign Flag PL (0) or Sign Flag Negative NG (1). Kung negative!
*sets when the most significant digit (leftmost bit) is equal to 1.
3. Parity - Parity Odd (0) or Parity Even (1). Even ba yung number of ones!
*Only changes AFTER AN ARITHMETIC OPERATION.
*The parity flag indicates whether there are an even or odd number of bit set in the data.
CPE 005B Page 4

*The parity flag indicates whether there are an even or odd number of bit set in the data.
*Convert the result to binary to find out if it's parity even or parity odd.

Example:
1) Result: 0000 --> #1s = 0. Parity even!
2) Result: FC1F --> 1111 / 1100 / 0001 / 1111. #1s = 11. Parity odd!
4. Auxiliary Carry - No Auxiliary Carry NA (0) or Auxiliary Carry AC (1). Kung may carry sa gitna!
*For 8 bits, sets if there's a carry from 4th to 5th bit.
*For 16 bits, sets if there's a carry from 8th to 9th bit.
*For 32 bits, sets if there's a carry from 16th to 17th bit.

Example:
AX = 1111 0101
+1111 1010
0110 1111
*Turns on the carry flag, but no auxiliary carry.
AX = 1111 1111
+0000 0001
0000 0000
*Turns on the carry flag and the auxiliary carry.
5. Overflow Flag - No Overflow NV (0) or Overflow OV (1). For signed numbers only!
*Kailangang parehong 1 or 0 ang addends, tapos kabaliktaran ang result. 1 +1 = 0 or 0 + 0 =1
Rules for Overflow Flags:
1. If the sum of two numbers with the sign bits off yields a result number with the sign bit on, the "overflow" flag is turned on.
*0100 + 0100 = 1000 (overflow flag is turned on)
2. If the sum of two numbers with the sign bits on yields a result number with the sign bit off, the "overflow" flag is turned on.
*1000 + 1000 = 0000 (overflow flag is turned on)
* Note that you only need to look at the sign bits (leftmost) of the three numbers to decide if the overflow flag is turned on or off.

6. Zero: No Zero NZ(0) or Zero ZR (1). Kung equal to zero yung value!
*Tells us if the result is equal to zero.
*1000 - 1000 = 0000 (ZR)

CPE 005B Page 5

Basic Structure of a Computer


Monday, December 01, 2014

12:47 AM

*When we say basic structure, we refer to the computer's architecture.


Factors that affect Architecture:
1. Speed or Clock Rate
a. Before, data transmission is half duplex only, one goes north or one goes south,
but now the data road is widening!
2. Memory
a. Number of storage location
Computers from most powerful to least powerful
1. Super computers
2. Mainframe computers
3. Mini computers
4. Micro computers
*We will talk about microcomputer!
*It uses microprocessor as its central processing unit.
History (Generation of Computers)
1. Vacuum Tubes - bulky, needs airconditioning unit
2. Transistor and Diodes - reduction of size but still big and expensive
3. Integrated Circuit
a. Small Scale Integration (SSI) - 1 - 50 transistors
b. Medium Scale Integration (MSI)
c. Large Scale Integration (LSI)
d. Very Large Scale Integration (VLSI)
4. Microprocessor - used for single user computer
5. Robotics and AI
*Decrease in size and costs, yet increase in speed and capability
*Silicon Valley in San Francisco, California.
*Microns - Smallest wire on an IC. 100 microns is approximately as large as a strand of hair

What's inside a microcomputer?


1. I / O (input and output)
a. Input: keyboard, mouse, switch, joystick, keypad, mousepad
b. Output: Monitor, printer, speaker
*monitor can also be both an input and outpuet
2. CPU (Central Processing Unit)
a. CU and ALU are registers on the computer.
b. This part houses the Arithmetic Logic Unit (ALU), which is responsible for
arithmetic operations (MDAS) and logical operation (AND/OR, NOT)
c. This part also houses the Central Unit, which controls the operation of the
computer

CPE 005B Page 6

Buses are modes of data transmission on computers.


2 kinds of bus
1. System bus
2. Internal bus
3 Kinds of Bus
1. Data bus - contains the computer data (bi directional)
2. Address bus - contains address or location (unidirectional, from CPU to Memory / IO)
3. Control bus - controls the syncing of data transmission

CPE 005B Page 7

CPE 005B Page 8

Data Representation
Monday, December 15, 2014

9:18 PM

Bit (Binary Digit) - 0, 1 (smallest unit that represents data inside the computer)
* Most significant bit - left most bit
* Least significant bit - right most bit
*Signed bit = - (1) or + (0)
*Unsigned bit = +
Bytes: 8 bits, basic storage unit for IA-32-based computer
Words: 16 bits
Double Words: 32 bits
Quad Words: 64 bits
Storage Type Unsigned Range Signed Range
Byte

0 --> 28-1

-27 --> (27-1)

Word

0 --> 216-1

-215 --> (215-1)

Double Word 0 --> 232-1

-231 --> (231-1)

0 --> 264-1

-263 --> (263-1)

Quad Word

Measurements

Number of Bytes

Kilobyte

210

Megabyte

220

Gigabyte

230

Terabyte

240

Petabyte

250

Exabyte

260

Zettabyte

270

Yottabyte

280

CPE 005B Page 9

Number System
Monday, December 15, 2014

11:26 PM

*Converting Hexadecimal to 1s complement = Subtract 15 per digit


*Negative signed hexadecimal = greater than 8 yung MSD or leftmost.
*Kapag subtraction, convert to 2s complement. Tanggalin ang MSD and iadd.

CPE 005B Page 10

Lab Activities
Monday, December 15, 2014

11:48 PM

-T: Examine the contents of the registers.


-R AX: Modify values of AX
-R F: Modify values of flags
Know the difference:
* mov al,00
* sub al,01
=ax: 00FF
*mov ax,0000
* sub ax,0001
= ax:FFFF
*Setting ax or any register to 0000 doesn't trigger the or Parity flags.

CPE 005B Page 11

3.1 Basic Elements of Assembly Language


Debug has many limitations:
* It does not let you create symbolic names, or insert and delete individual source code lines.
Elements of Assembly Language
1. Numeric Literal - combination of digits, signs, a decimal point and an exponent. (e.g. 5, 5.5, -5.5, 26. E+0.5)
a. Integer constant can end with Radix symbols: h (hexadecimal), q or o (octal), d or no radix (decimal) and b
(binary)
b. Upper case or lower case do not matter.
c. When a hexadecimal constant begins with a letter, a leading zero must be placed.
d. Constant expression consists of combinations of numeric literals, operators and defined symbolic
constants. The expression value must be able to be determined at assembly time, and its value cannot
change at the runtime. (e.g 4*20, -3*4/6)
e. Symbolic constant created by assigning a constant expression to a name. (e.g. rows = 5, columns = 10,
tablePOS= rows*columns). *Can only be evaluated at assembly time.
f. Character or String Constant - a constant may also represent a string of characters in either single or
double quotation marks. Embedded quotes are permitted. (e.g. 'ABC', "X"). Each characters is one byte
long.
2. Statements
a. Statements are free form, meaning that they can be written in any column with any number of spaces
between each operand.
b. Statements should be written on a single line and cannot pass column 128, unless with the use of
backslash.

c. Two classes of Statements


i. instructions - statements executed by the processor at runtime.

ii. Directives - statements that provide information to tell the assembler how to generate executable
code. A statement that affects either the program listing or the way machine code is generated.
1) Count DB 50 directive tells the assembler to create a storage for a byte variable named count
and initialize it to 50.
2) .Stack 4096 directive tells the assembler to reserve 4096 bytes of stack space.
3. Names: identifies a label, variable, symbol or keyword. It may contain any of the following characters:

CPE 005B Page 12

a.
b.
c.
d.

Max of 247 characters in MASM


No distinction between uppercase and lower case letters.
Can start with @, letter, _ or $, but avoid @ since it is associated with other predefined symbols.
Variable - a location in a program's data area that has been assigned a name. (count1 db 50; a variable
(memory allocation)
e. Label - place markers when a program needs to jump or loop from one location to another.

f. Keyword. A keyword always has some predefined meaning to the assembler. (mov, proc, title, add, ax,
END)

CPE 005B Page 13

3.2 Sample Hello Program


Segments: building blocks of programs
1. Code Segments: where program instructions are stored
2. Data Segments: contains all variables
3. Stack Segments: contains the program's runtime stack

*.model small - a directive that indicates that uses a type of structure in which the program
uses no more than 64K of memory for code, and 64K for data.
*.stack 100h - a directive that sets aside 100h (256) bytes of stack space for the program
*.data - a directive that marks the beginning of the data segment where variables are stored.
The assembler allocates a block of memory to hold the string containing "Hello,world!" along
with two bytes containing a newline character sequence (odh, 0ah). String terminator ($).
*.code marks the beginning of the code segment where the executable instructions are
located.
*PROC directive declares the beginning of a procedure. Main is the procedure!
*mov ax, @data / mov ds, ax --> To copy the address of the data segment into DS register.
*mov ah,09 --> display a character string.
*mov ax,4C00h / int 21h) --> halt the pgoram and return control of the operating system.z
*MAIN ENDP --> to end main proc
*end main --> ending

CPE 005B Page 14

CPE 005B Page 15

3.3 Assembling, Linking and Debugging


Sunday, January 11, 2015

11:15 PM

*Assembler: a utility program that converts a source program into an object file
*Linker: a program that converts object files into executable programs
Advantages of an assembler than debug:
1. Can use text editor.
2. Can use symbolic names for variables, rather than hard-coded numeric addresses.
Advantage of a Linker:
1. Programs can take advantage of existing libraries full of useful subroutines.

Listing file (optional) - produced by assembly. copy of the program's source file (suitable for printing)
with line numbers and translated machine code. To create a listing file, use the coding below.
Map file (optional) - produced by linker. Contains information about the program's code, data and stack
segments. To create a map file:
Link library - file containing subroutines that are already compiled into machine language

Warning Messages: will assemble but the object file may have errors.

CPE 005B Page 16

3.4 Data Allocation Directives


Variable - symbolic name for a location in memory where some data are stored.
Offset of a variable - distance from the beginning of the segment to the variable
.data
aList db "ABCD"

*If the first letter is at offset 0,the next one is at offset 1.


*the offset of aList is the same as the offset of its first letter.
Data Allocation Directives

1. Define Byte: For storage of one or more byte values.


Single Initializers:

*Question mark (?) assigned an undefined intitial contents.


Multiple Initializers:
*To know if it fits one byte, convert to hexadecimal!
*_len = computes the length of the string!

DUP Operator: duplicates or repeat one or more values when allocating space.
CPE 005B Page 17

DUP Operator: duplicates or repeat one or more values when allocating space.

2. Define Word - for storage of 2byte-values.

Pointer. The offset of a variable or subroutine can be stored in another variable, called a
pointer.

*When storing word, the first offset is given on the rightmost side.
3. Defined Doubleword (DD) - for storage of 4byte values.

CPE 005B Page 18

3.5 Symbolic Constants


Monday, January 12, 2015

12:50 AM

Equate Directives - allow constants and literals to be given symbolic names.


1. Equal-Sign Directive (or redefinable equate) - the equal sign directive creates an absolute symbol
by assigning the value of a number expression to a name. name = expression.
a. Allocates no storage (unlike DB, DW)
b. All occurrences of the name is replaced by the expression.

2. EQU directive - assigns a symbolic name to a string or numeric constant. Same as equal sign
directive, but cannot be redefined later in the program.

3. TEXTEQU directive

CPE 005B Page 19

3.6 - 3.7 Data Transfer and Arithmetic Instructions


1. Mov Instruction: copies data from one operand to another

* reg should be any non-segment register, except that IP cannot be a target operand.
* sizes of both operands must be the same.

*CS cannot be a target operand.


* mov ax, var1 / mov var2, ax --> to transfer contents from var1 to var2.

2. Operands with Displacements


*Direct Offset Addressing - lets you access memory values that do not have their own labels.
a. When dealing with db, DB + 1 means next character.
b. When dealing with dw, DW + 2 means next word.
c. When dealing with DD, DB+4 means next doubleword.
3. XCHG Instruction = exchange values.l

4. INC / DEC = +1 / -1

5. ADD / SUB Instruction


1. Sizes of operands (source and destination) must be the same.
2. Only one memory can be an operand. Not both.

CPE 005B Page 20

3.8 Basic Operand Types


Three Basic Types of Operands
1. Immediate - Constant
2. Register - one of the CPU registers
3. Memory - reference to a location in memory.
Displacement: either a number or the offset of a variable
Effective Address of an operand refers to the offset (distance) of the data from the beginning of its
segment.
Addressing mode - used by an instruction refers to the type of memory operand in use.
Address: 121 Maple Street.
Maple Street - house's base location
121 - offset from the beginning of the street.
1. Register Operands - CPU register and no memory access is required.

2. Immediate Operands - is a constant expression

3. Direct Operands: refers to the contents of memory at a location identified by a label in the data
segment.

CPE 005B Page 21

Offset Operator: returns the 16bit offset of a variable.

4. Direct-Offset Operands: the +/- adds or subtracts the offset.

CPE 005B Page 22

6.2.8 CMP Instruction


Wednesday, January 21, 2015

1:17 AM

CMP (CMP destination, source)


CMP reg, reg
CMP reg, imm/mem
*used to compare integers and character codes.
*implied subtraction between source and destination operand but neither of them is modified
*changes the Overflow, Sign, Zero, Carry, Auxiliary Carry and Parity Flags as if the subtraction really
happened.

Example:
*mov ax,5
cmp ax,10 --> ZF = 0, CF = 1
*mov ax,1000
mov cx,1000
cmp cx,ax --> ZF = 1, CF = 0

CPE 005B Page 23

AND Instruction
Wednesday, January 21, 2015

1:39 AM

*AND (destination, source)


- performs a boolean (bitwise) AND operation between each pair of matching bits in two operands and
places the result in the destination operand.

CPE 005B Page 24

Jcond Instruction
Wednesday, January 21, 2015

1:49 AM

Jcond (Jcond destination) or conditional jump


*refers to a flag condition identifying the state of one or more flags.
*these flags are commonly set by arithmetic, boolean and comparison instructions.
4 types:
1. Jumps based on specific flag values

N,

2. Jumps based on equality

CPE 005B Page 25

3. Jumps based on Unsigned Comparisons

CPE 005B Page 26

4. Jumps based on Signed Comparisons

CPE 005B Page 27

Answer: No (8109h is negative)

CPE 005B Page 28

M - Logical Instructions
Saturday, January 24, 2015

3:18 PM

Logical Instructions (AND, OR, XOR, TEST)


- performs bitwise operation and places the result on the destination operand
*Syntax:
1. reg, reg
2. reg, mem
3. reg, imm
4. mem, reg
5. mem, imm
* memory to memory and immediate destination is not allowed!
* operands must be of the same size!
*Truth Tables:
X

AND / TEST

OR

XOR

*AND / TEST = both operands should be 1


*OR = either operands or both should be 1
*XOR = exactly one should be 1
Flags:
*clears overflow and carry flags and modifies sign, zero and parity depending on the results of the instruction in the
destination operand
AND Instruction (AND destination, source) - Intersection
1. can be used to clear out or zero (called bit masking) certain bits in a byte or word
Example 1:
mov al, 33h ; 33H --> 0011 0011
AND al, 0CFh; 0CFh --> 1100 1111
Result: 0000 0011
Example 2:
Suppose, for example, that a control byte is about to be copied from the AL register to a hardware device. Further, we
will assume that the device resets itself when bits 0 and 3 are cleared in the control byte. Assuming that we want to
reset the device without modifying any other bits in AL, we can write the following:
*AND AL,11110110b ; clear bits 0 and 3, leave others unchanged.
Example 3:

CPE 005B Page 29

2. provides an easy way to translate a letter from lower case to upper case. To convert a lower case character to an
upper case letter, use AND x, 11011111b
Example:
0110 0001 - a
0100 0001 - A
OR Instruction (OR destination, source) - Union
1. can be used to set certain bit to 1 in a byte or word
Example 1:
mov dl,03 ; 03 --> 0011
OR dl, 20h ; 20h --> 10 0000
Result: 10 0011
Example 2:
Suppose, for example, that your computer is attached to a servo motor, which is activated by setting bit 2 in its
control byte. Assuming that the AL register contains a control byte in which each bit contains some important
information, the following code only sets the bit in position 2.
OR AL,00000100b ; set bit 2, leave others unchanged
Example 3:

2.Flag statuses:
If you OR the register itself (e.g OR AL,AL), you will know the value based on the zero flag and sign flag.

XOR Instruction (XOR destination, source)


1. Ideal tool for simple data encryption since it reverses itself when applied twice to the same operand!
Example:
Step 1: XOR AL, 1010 0001 --> If al = 1011 1010, then al becomes 0001 1011
Step 2: XOR AL, 1010 0001--> Since al is 0001 1001, it becomes 1011 1010. Al goes back to the initial value!

2. Any register / memory XORed to itself is equal to zero. (e.g XOR al,al)
3. To initialize zero in a register, XOR AL, AL is faster in 8086 / 8088 processors than using mov al,00h.
3. To check parity, you can XOR a number with a zero. (XOR X,0)

CPE 005B Page 30

TEST instruction
1. similar to AND instruction except that destination value does not change, but flags status are changed.
2. valuable in finding out if individual bits in an operand are set
Example:
Testing Multiple Bits The TEST instruction can check several bits at once. Suppose we want to know whether bit 0 or
bit 3 is set in the AL register. We can use the following instruction to find this out:
Test al, 0000 1000b --> tests bits zero and 3 if they are set or clear
0 0 1 0 0 1 0 1 <- input value
0 0 0 0 1 0 0 1 <- test value
0 0 0 0 0 0 0 1 <- result : ZF = 0
0 0 1 0 0 1 0 0 <- input value
0 0 0 0 1 0 0 1 <- test value
0 0 0 0 0 0 0 0 <- result: ZF = 1
NOT Instruction (NOT reg/mem) - Compliment
- reverses all bits / gets 1 compliment.
- cannot toggle imm data.
- no flags are affected!

CPE 005B Page 31

F - Shift and Rotate Instructions


Monday, February 02, 2015

10:04 PM

*Shifting - to move bits right and left inside an operand.


*It affects both Carry and Overflow Flags.

Two Ways to Shift an operand's bits:


1. Logical Shift - fills the newly created bit position with zero. Maglalagay ng zero sa MSD at uusog to the right. Yung last digit
mapupunta sa Carry Flag.

Example: 11001111 --> 01100111

2. Arithmetic Shift - the newly created bit position is filled with a copy of the original number's sign bit. Si MSD will be repeated twice
sa unahan.

Example: 11001111 --> 11100111

Syntax:
1. reg, imm
CPE 005B Page 32

1. reg, imm
2. mem, imm
3. reg, CL register
4. mem, CL register
*where imm is any integer between 0 and 255.
1. SHL Instruction
*The SHL (shift left) instruction performs a logical shift on the destination operand, filling the lowest bit with 0. The highest bit is
moved to the Carry flag and the bit that was initially in the Carry flag is discarded.
*MSD becomes the new Carry Flag.
*LSD becomes zero.
*If it is shifted more than once, the last MSD becomes the CF.

Example: SHL 11001111 by 1 bit --> 10011110

Example:

Bitwise Multiplication:
*SHL can perform multiplication by powers of 2. Shifting any operand left by n bits multiplies the operand by 2n.
Example 1: When we SHL 5 by one bit, 5 * 2 = 10

Example 2: When we SHL 10 by 2 bits, 10*22 = 40

2. SHR Instruction:
*performs a logical right shift on the destination operand, replacing the highest bit with a 0. The lowest bit is copied into the
Carry flag, and the bit that was previously in the Carry flag is lost.
*MSD becomes zero.
*LSD becomes the Carry Flag.
*In a multiple shift operation, the last bit to be shifted out of position 0 (the LSB) ends up in the Carry flag.

Example 1:

Bitwise Division: Logically shifting an unsigned integer to the right by n bits divides the operand by 2n.

Example 1: 32 was shifted once. 32 / 2 = 16.

CPE 005B Page 33

Example 2:

3. SAL and SAR Instructions:


a. SAL: Shift arithmetic left
*works the same as the SHL instruction.
*for each shift count, SAL shifts each bit in the destination operand to the next highest bit position. The lowest bit is assigned 0.
The highest bit is moved to the Carry flag, and the bit that was in the Carry flag is discarded.

Example: Shifting 11001111 by 1 bit to the left --> 10011110

b. SAR: Shift Arithmetic Right


*different than SHR
*performs a right arithmetic, repeats MSD on the left.

Example 1: When -16 is SAR by 1, -16/2 = -8

Signed Division: You can divide a signed operand by a power of 2, using the SAR instruction.
Example 1: When you SAR -128 by 3 bits, -128 / 23 = -16

Sign-Extend AX into EAX: Suppose AX contains a signed integer and you want to extend its sign into EAX. First shift EAX 16 bits to
the left, then shift it arithmetically 16 bits to the right.

4. ROL Instruction:
*The ROL (rotate left) instruction shifts each bit to the left. The highest bit is copied into the Carry flag and the lowest bit position.
The instruction format is the same as for SHL.

Example 1:

CPE 005B Page 34

Example 2:

Exchanging Groups of Bits: Use ROL to exchange the upper bits (bits 4-7) and lower bits (0-3) halves of a byte.
Example 1: 26h rotated 4 times becomes 62h

Example 2: When ROL is applied by 4 bits, the characters goes to the left.

5. ROR Instruction
*ROR (rotate right) instruction shifts each bit to n right and copies the lowest bit into the Carry flag and the highest bit position.

Example 1:

6. RCL and RCR Instructions:


*RCL: shifts each bit to the left, copies the carry flag to the LSB and copies the MSB into the Carry flag.

Example 1:

Example 2:

RCR Instruction:
*shifts each bit to the right, copies the Carry flag into the MSB and copies the LSB into the Carry flag.

CPE 005B Page 35

CPE 005B Page 36

F - Screen Attributes
*Screen has 80 columns (0 - 79 ) and 25 (0-24) rows.
Int 10h / Function 06h - handles the processing of clearing the screen
* mov ah,06h = function
* al = number of blank lines at the bottom of the window
* cl = column number start
* dl = column number end
* ch = row number start
* dh = row number end
* bh = display attributes for blank lines

Int 10h / Function 02h


*mov ah, 02h = function
*dl = column position (y)
*dh = row position (x)
*bh = page number

CPE 005B Page 37

CPE 005B Page 38

CPE 005B Page 39

Вам также может понравиться