Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
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.
C++
; 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
.Code
   Start:
      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
         Ret

      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
         Ret

      Exit:
         Mov AH, 4CH
         Int 21H
   End Start
Posted
Updated 28-Sep-12 23:03pm
v3
Comments
Richard MacCutchan 29-Sep-12 5:04am    
I added a comment to Getc to show what it is doing.

1 solution

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]
 
Share this answer
 
v2
Comments
[no name] 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900