Click here to Skip to main content
13,146,014 members (45,011 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Could you please let me know the meaning of using PUSH and Pop instructions in the following program.
1. Why are they necessarily required in functions Getc and Putc
2. In function Getc why Push AX and Pop AX is not coded and while the
same is coded in Putc

Any help would be appreciated.

Below is the code for your reference.
; program to get a character from a keyboard and then print to the screen
; Assembler: Tasm
; Operating system: MS-DOS 6.2
; compilation: Tasm test.asm
; linking:     tlink test.obj
; execution:   test.exe
.Model Small
.Stack 100H
      Call Getc
      Call Putc
      Call Exit

      Getc:     ; gets a character from user
         ; this routine gets a character into the AX register
         Push BX
         Push CX
         Push DX
         Mov AH, 01H
         Int 21H
         Pop  DX
         Pop  CX
         Pop  BX
      Putc:     ; prints a character to the screen
         Push AX
         Push BX
         Push CX
         Push DX
         Mov  DL, AL
         Mov  AH, 02H
         Int 21H
         Pop  DX
         Pop  CX
         Pop  BX
         Pop  AX
         Mov AH, 4CH
         Int 21H
   End Start
Posted 28-Sep-12 21:46pm
Updated 28-Sep-12 23:03pm
Richard MacCutchan 29-Sep-12 5:04am
I added a comment to Getc to show what it is doing.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

PUSH saves a register to the stack, POP restores it.

The routines you show save the external registers because they need to use them for their own purposes, and do not want to corrupt the registers for the calling program. If they didn't do it, then the calling program would have to do it before and after each call, which is both inefficient, and dangerous - if you forget for just one instance of the call, you will get corrupted registers, and your program will not work.

It is pretty normal for a routine to save it's working registers in this way.

[edit]I lost a letter! - OriginalGriff[/edit]
Zerr0cool 29-Sep-12 3:55am
Thank you. But why in function Getc Push AX and Pop AX are not coded? Can you please also explain its significance.
OriginalGriff 29-Sep-12 4:33am
The clue is in the function name - Getc - Get Character.

Where do you think it returns the character it has just got? :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100