Successfully reported this slideshow.

Microprocessor:stack,shifting,looping

0

Share

Upcoming SlideShare
Computer network
Computer network
Loading in …3
×
1 of 21
1 of 21

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Related Audiobooks

Free with a 14 day trial from Scribd

See all

Microprocessor:stack,shifting,looping

  1. 1. Microprocessor S TAC K, L OOP AND SHI F T ING
  2. 2. The Shift Instruction The 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
  3. 3. The Shift Family There 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
  4. 4. Multiplication & Division by Shift Multiplication by left shift  A left shift by 1 bit doubles the destination value, i.e. multiplies it by 2. Division by right shift  A 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
  5. 5. Shift Examples fahad
  6. 6. 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
  7. 7. Stack count
  8. 8. PUSH PUSH: 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
  9. 9. PUSH - Example
  10. 10. 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
  11. 11. POP - Example
  12. 12. STACK Application To store and restore registers To reverse an input fahad
  13. 13. Looping Structures A 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
  14. 14. FOR LOOP The loop statements is repeated a known number of times. In pseudo code, For loop_count times do (using CX) Statements END_FOR fahad
  15. 15. Example of For Loop For 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
  16. 16. WHILE LOOP This loop depend on a condition, in pseudocode, WHILE condition DO Statements END_ WHILE fahad
  17. 17. Example of WHILE Loop WHILE 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
  18. 18. REPEAT LOOP Another conditional loop is the REPEAT LOOP. In pseudocode, REPEAT Statements UNTIL condition fahad
  19. 19. Example of REPEAT Loop REPEAT 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
  20. 20. WHILE Vs REPEAT The 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

×