Example on passing parameters through stack, Computer Engineering

Assignment Help:

Q. Example on Passing Parameters through Stack?

PROGRAM: Version 3

DATA_SEG               SEGMENT

                        BCD DB 25h; Storage for BCD test value

BIN DB?  ; Storage for binary value

DATA_SEG               ENDS

STACK_SEG   SEGMENT      STACK

DW 100 DUP (0); Stack of 100 words

TOP_STACK   LABEL    WORD

STACK_SEG   ENDS

CODE_SEG SEGMENT

ASSUME CS: CODE_SEG, DS: DATA_SEG, SS: STACK_SEG

START:           MOV AX, DATA                  ; Initialise data segment

 

MOV DS, AX                                    ; using AX register

MOV AX, STACK-SEG.      ; initialise stack segment

MOV SS, AX                         ; using AX register

MOV SP, OFFSET TOP_STACK; initialise stack pointer

MOV AL, BCD                      ; Move BCD value into AL

PUSH AX                               ; and push it onto word stack

CALL BCD_BINARY          ; Do the conversion

POP AX                                  ; Get the binary value

MOV BIN, AL                       ; and save it

NOP                                        ; Continue with program

 

; PROCEDURE: BCD_BINARY Converts BCD numbers to binary.

; INPUT   : None - BCD value assumed to be on stack before call

; OUTPUT   : None - Binary value on top of stack after return 

; DESTROYS: Nothing

BCD_BINARY PROC NEAR

                                 PUSHF     ; Save flags

                                 PUSH AX; and registers: AX

                                 PUSH BX; BX

                                 PUSH CX; CX

                                 PUSH BP; BP. Why BP?

                                 MOV BP, SP; Make a copy of the

                                                ; stack pointer in BP

                                 MOV   AX, [BP+ 12]; Get BCD number from

                                                ; stack. But why it is on 

 

; BP+12 location? Please note 5 PUSH statements + 1 call which is intra-segment (so 

; just IP is stored) so total 6 words are pushed after AX has been pushed and since it is 

; a word stack so the BCD value is stored on 6 × 2 = 12 locations under stack. Hence 

; [BP + 12] (refer to the figure given on next page).

MOV BL, AL; Save copy of BCD in BL

AND BL, 0Fh; mask lower 4 bits

AND AL, F0H; Separate upper 4 bits

MOV CL, 04; Move upper BCD digit to low

ROR   AL, CL; position BCD digit for multiply location

MOV   BH, 0Ah   ; Load 10 in BH

MUL BH; Multiply upper BCD digit in AL by 10

   ; The result is in AL

ADD   AL, BL; Add lower BCD digit to result.

MOV [BP + 12], AX   ; Put binary result on stack

; Restore flags and registers

POP BP

POP CX

POP BX

            POP AX

                                 POPF  

                                 RET  

BCD_BINARY       ENDP

CODE_SEG            ENDS

END   START  

 

Discussion: 

The parameter is pushed on stack before the procedure call. Procedure call causes the current instruction pointer to be pushed on to stack. In the procedure flags AX, BX, CX and BP registers are also pushed in that order. Hence the stack looks to be:

721_Example on Passing Parameters through Stack.png

The instruction MOV BP, SP transfers contents of the SP to BP register. Now BP is used to access any location in stack by adding appropriate offset to it. For illustration MOV AX, [BP + 12] instruction transfers the word starting at the 12th byte from the top of stack to AX register. It doesn't change the contents of BP register or top of the stack. It copies pushed value of AL and AH at offset 008Eh into the AX register. This instruction isn't equivalent to POP instruction. 

Stacks are useful for writing procedures for multi-user system programs or recursive procedures. It's a decent practice to make a stack diagram as above when using procedure call through stacks. This helps in reducing errors in programming.


Related Discussions:- Example on passing parameters through stack

Name some popular storage devices, Name some popular storage devices In...

Name some popular storage devices In contrasts with storage, which 1) retains data and programs irrespective of whether they are currently in use or not, (2) retains data

What is collection objects in vb, Likewise to arrays but is preferred over ...

Likewise to arrays but is preferred over an array due to of the following reasons. 1. A collection objects uses less Memory than an array. 2. It gives methods to add and dele

Detail explanation of cryptographic modules, Detail explanation of Cryptogr...

Detail explanation of Cryptographic modules Physical security of the cryptographic modules is also built in order to provide total security of the whole system and protect from

Uniform memory access model (uma), Uniform Memory Access Model (UMA) In...

Uniform Memory Access Model (UMA) In this model, the main memory is uniformly shared by all processors in multiprocessor systems and each processor has equal access time to sha

Which way do twin twist props on a boat rotate, Q. Which way do twin twist ...

Q. Which way do twin twist props on a boat rotate when propelling the boat forward? Answer:- In opposite direction however they are designed to propel the boat forward eve

What are micrographics, What are micrographics? A micrographic is an im...

What are micrographics? A micrographic is an image or photographic reproduction of an object which is then changed to film. Micrographics are frequently used for permanent reco

How are interrupt handled by the operating system, How are interrupt hand...

How are interrupt handled by the operating system? The fundamental interrupt mechanism works as follows: The CPU hardware has wire called the interrupt-request line which

Control traffic flowing in and out of the network, Firewalls use one or mor...

Firewalls use one or more of three methods to control traffic flowing in and out of the network:  Packet filtering - Packets (small chunks of data) are examined against a s

Describe the structure and function of a computer system, Question: a) ...

Question: a) Describe the structure and function of a computer system. b) The raw speed of a microprocessor will not achieve its potential unless a constant stream of wor

Determine the use of loop instruction, Q. Determine the use of LOOP instruc...

Q. Determine the use of LOOP instruction? Program: This program prints the alphabets (A-Z) ; Register used: AX, CX, DX CODE SEGMENT ASSUME: CS: CODE.     MAINP:

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd