Upload
samiul-ehsan
View
57
Download
0
Tags:
Embed Size (px)
Citation preview
The Shift InstructionThe shift Instructions shift the bits in the destination operand by one or more position either to the left or right.
For a shift instruction, the bits shifted out or lost.
For a single shift, the form is
Opcode destination, 1
For a shift of N positions, the form is
Opcode destination, CL
The Shift FamilyThere are two different sets of shift instructions
One set for doubling and halving unsigned binary numbers
SHL (Shift Left) – doubles
SHR (Shift Right) - halves
The other for doubling and halving signed binary numbers
SAL (Arithmetic Shift Left) – doubles
SAR (Arithmetic Shift Right) – halves
fahad
Multiplication & Division by ShiftMultiplication by left shiftA left shift by 1 bit doubles the destination value, i.e. multiplies it by 2.
Division by right shiftA right shift by 1 bit halves it and rounds down to the nearest integer, i.e.
divides it by 2.
But for a sign interpretation, SAR must be used instead of SHR.
fahad
STACK One dimensional data structure
Items are added and removed from one end of the structure i.e. Top of the Stack
Last In First Out Structure
STACK directive used to reserve a block of memory for stack. The syntax is:
STACK 100h
When program is assembled and load in memory:
SS (Stack Segment) Register: holds stack Segment Address
SP (Stack Pointer) Register: initialized to the value specified with the STACK directive, represents empty stack position
When stack is not empty, SP represents the top of the Stack
Stack grows toward the beginning of the memory.
fahad
PUSHPUSH: Used to add a new source (16-bit register or memory word) on stack
Syntax:
PUSH source
Execution of PUSH causes:
SP is decreased by 2
A copy of source contents is moved to the address specified by SS:SP.
Source remains unchanged
fahad
POP POP: Used to remove top item from stack to destination (i.e. a 16-bit register or memory word).
Syntax:
POP destination
Execution of POP causes:
The contents of SS:SP (top of the stack) is moved to the destination.
SP is increased by 2
fahad
Looping StructuresA loop is a sequence of instructions that is repeated.
The Number of times to repeat may be known in advance, or it may depend on condition.
The counter register CX initialized to loop_count.
Execution of LOOP instruction causes CX to be decremented automatically.
fahad
FOR LOOPThe loop statements is repeated a known number of times.
In pseudo code,
For loop_count times do (using CX)
Statements
END_FOR
fahad
Example of For LoopFor loop to display a row of 80 stars :
MOV CX,80 ; number of stars to display
MOV AH,2 ; display character function
MOV DL, “ * ” ; character to display
FOR:
INT 21H ; display a star
Loop FOR ; repeat 80 times
fahad
WHILE LOOPThis loop depend on a condition, in pseudocode,
WHILE condition DO
Statements
END_ WHILE
fahad
Example of WHILE LoopWHILE Loop to count the number of characters in an input line :
MOV DX,0 ; DX counts characters
MOV AH,1 ; prepare to read
INT 21H ; character in AL
WHILE:
CMP AL, 0DH ; Cursor?
JE END_WHILE ; yes, exit
INC DX ; not Cursor, increment count
INT 21H ; read a character
JMP WHILE ; loop back
END_WHILE:
fahad
REPEAT LOOPAnother conditional loop is the REPEAT LOOP. In pseudocode,
REPEAT
Statements
UNTIL condition
fahad
Example of REPEAT LoopREPEAT Loop to read character until a blank is read :
MOV AH, 1 ; prepare to read
REPEAT:
INT 21H ; char in AL
; until
CMP AL, “ ” ; a blank?
JNE REPEAT ; no, keep reading
fahad
WHILE Vs REPEATThe advantage of WHILE is that the loop can be bypassed if the terminating condition is initially false.
Whereas the statements in a REPEAT must be done at least once.
The code for a REPEAT loop is likely to be little shorter as there is only a conditional jump at the end.
Whereas the WHILE loop is little longer as there has two conditional jumps at the top and at the end.
fahad