INTEL 8086 PROCEDURES
Intel 8086 Procedures
     Procedure is a part of code that can be called from a program in order to make some specific task.
     Procedures make program more structural and easier to understand.
     Generally procedure returns to the same point from where it was called.
The syntax for procedure declaration:
      Name PROC
          ; here goes the code
          ; of the procedure ...
      RET
      name ENDP
 Name - is the procedure name, the same name should be in the top and the bottom, this is used to
  check correct closing of procedures.
                                        INTEL 8086 PROCEDURES
 BL registers, multiplies these parameters and returns the result in AX register;
  Example:
     ORG 100h
                                                  • In this example, value of AL register is updated
     MOV AL, 1
     MOV BL, 2                                      every time the procedure is called
     CALL   m2                                    • BL register stays unchanged
     CALL   m2
     CALL   m2                                    • so this algorithm calculates 2 in power of 4
     CALL   m2
     RET         ; return to operating system.    • so final result in AX register is 16 (or 10h).
     m2 PROC
     MUL BL         ; AX = AL * BL.
     RET         ; return to caller.
     m2 ENDP
     END
Here goes another example that uses a procedure to print a Hello World! message:
    ORG 100h
    LEA SI, msg                ; load address of msg to SI.
    CALL print_me
    RET                        ; return to operating system.
    print_me   PROC
                                                                           • "b." - prefix before [SI]
    next_char:                                                               means that there is need to
      CMP b.[SI], 0            ; check for zero to stop
      JE stop                                                                compare bytes, not words.
      MOV AL, [SI]              ; next get ASCII char.
                                                                           • When comparing words
                                                                             add "w." prefix instead. b
      MOV AH, 0Eh               ; teletype function number.
      INT 10h                   ; using interrupt to print a char in AL.
      ADD SI, 1                 ; advance index of string array.
      JMP next_char            ; go back, and type another char.
    stop:
    RET                         ; return to caller.
    print_me   ENDP
    msg DB 'Hello World!', 0    ; null terminated string.
    END
                                     INTEL 8086 STACK
The Stack:
        Stack is an area of memory for keeping temporary data.
        Stack is used by CALL instruction to keep return address for procedure
        RET instruction gets this value from the stack and returns to that offset.
        Stack can be used to keep any other data.
        There are two instructions that work with the stack:
              • PUSH - stores 16 bit value in the stack.
              • POP - gets 16 bit value from the stack.
                                       INTEL 8086 STACK
 The Intel 8086 stack uses LIFO (Last In First Out) algorithm
 This means that if 1, 2, 3, 4, 5 are PUSH into the stack one by one, the first value that will get
  POP will be 5, then 4, 3, 2, and only then 1.
                                      It is very important to do equal number of PUSHs and POPs,
E. g.
                                       otherwise the stack may be corrupted and it will be impossible
                                       to return to operating system.
                                      PUSH and POP instruction are especially useful because there
                                       are not too much registers to operate with, so here is a trick:
                                             •Store original value of the register in stack (using PUSH).
                                            •Restore the original value of the register from stack (using
                                          POP).
                                   INTEL 8086 STACK
The stack memory area is set by SS (Stack Segment) register, and SP (Stack Pointer) register.
Generally operating system sets values of these registers on program start.
"PUSH source" instruction does the following:
•     Subtract 2 from SP register.
•     Write the value of source to the address SS:SP.
"POP destination" instruction does the following:
•     Write the value at the address SS:SP to destination.
•     Add 2 to SP register.
The current address pointed by SS:SP is called the top of the stack.
                                         INTEL 8086 STACK
Program using PUSH Command                                   Program using POP Command
Syntax for PUSH instruction:
PUSH REG                                            Syntax for POP instruction:
PUSH SREG                                           POP REG
PUSH memory                                         POP SREG
PUSH immediate                                      POP memory
REG: AX, BX, CX, DX, DI, SI, BP, SP.                REG: AX, BX, CX, DX, DI, SI, BP, SP.
SREG: DS, ES, SS, CS.                               SREG: DS, ES, SS, (except CS).
memory: [BX], [BX+SI+7], 16 bit variable, etc...    memory: [BX], [BX+SI+7], 16 bit variable, etc...
immediate: 5, -24, 3Fh, 10001101b, etc...
  Notes:
           •    PUSH and POP work with 16 bit values only
           •    PUSH immediate works only on 80186 CPU and later!
                                       INTEL 8086 STACK
                                                    Another example use of the stack for exchanging values.
Program example of using PUSH and POP Command
                                                     ORG 100h
ORG 100h
                                                     MOV AX, 1212h ; store 1212h in AX.
MOV AX, 1234h                                        MOV BX, 3434h ; store 3434h in BX
PUSH AX    ; store value of AX in stack.
MOV AX, 5678h ; modify the AX value.                 PUSH AX         ; store value of AX in stack.
                                                     PUSH BX         ; store value of BX in stack.
POP AX        ; restore the original value of AX.
                                                     POP AX         ; set AX to original value of BX.
RET                                                  POP BX         ; set BX to original value of AX.
END                                                  RET
                                                     END