Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASM
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.
; 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 28-Sep-12 21:46pm
Edited 28-Sep-12 23:03pm
v3
Comments
Richard MacCutchan at 29-Sep-12 5:04am
   
I added a comment to Getc to show what it is doing.

1 solution

Rate this: bad
good
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]
  Permalink  
v2
Comments
Zerr0cool at 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 at 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
0 Gihan Liyanage 392
1 ClimerChinna 247
2 Sergey Alexandrovich Kryukov 225
3 vikinghunter 176
4 OriginalGriff 161
0 Sergey Alexandrovich Kryukov 8,373
1 OriginalGriff 7,112
2 CPallini 2,598
3 Richard MacCutchan 2,025
4 Abhinav S 1,788


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2014
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