L
L
Lal Qarga2021-02-27 15:41:17
assembler
Lal Qarga, 2021-02-27 15:41:17

Why is the "push ax" command used 2 times?

Good afternoon. Please help. I'm learning Assembler. In the book "Assembler? It's Easy! Learning to Program" the push ax command is used 2 times in the code. Why exactly 2 times? Isn't it possible to just specify the push ax command once?

In general, the program does the following:  waits for the user to press any key;
- if it is an extended ASCII code (F1-F12, arrow buttons), then ignores it;
- if it is not extended ASCII (A-Z, 0-9, etc.) - fills the screen with the given character;
- if we press a key (27 or 1Bh), it fills the screen with spaces (mov al, 32) and exits.

CSEG segment 
assume cs:CSEG, ds:CSEG, es:CSEG, ss:CSEG 
org 100h 
Begin: 
 call Wait_key 
 cmp al,27 
 je Quit_prog 
 cmp al,0 
 je Begin 
 
 call Out_char 
 jmp Begin 
 
Quit_prog: 
 mov al,32 
call Out_char 
 int 20h 
 
; === Подпрограммы === 
; --- Wait_key --- 
Wait_key proc 
 mov ah,10h 
 int 16h 
 ret 
Wait_key endp 
 
; --- Out_char --- 
Out_char proc 
 push cx 
 push ax ; 1 раз
 push es 
 
 push ax ; 2 раз
 mov ax,0B800h 
 mov es,ax 
 mov di,0 
 mov cx,2000 
 pop ax 
 mov ah,31 
Next_sym: 
 mov es:[di],ax 
 inc di 
 inc di 
 loop Next_sym 
 
 pop es 
 pop ax 
 pop cx 
 ret 
Out_char endp 
 
CSEG ends 
end Begin

603a4ed397329085340661.jpeg
603a4ef3838d5624095397.jpeg

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2021-02-27
@Oghuzkhan2020

The first time - saving registers when entering the Out_char subroutine.
The second time is saving case before using it to change ES.

V
Vindicar, 2021-02-27
@Vindicar

Note that two calls to push ax correspond to two calls to pop ax.
Push saves a register on the stack by moving the top of the stack. Pop pops the value from the top of the stack into a register and pushes the top back. Therefore, the push-use-pop pattern is quite common if we need a case, but we don't want to lose its previous value.
In your case, this pattern occurred twice, one inside the other. The outer saves the register on entry to the subroutine, and restores it on exit (to less disturb the calling subroutine). Internal - Preserve AX case before forwarding, since we can't directly write to ES, only through another case.

E
Evgeny Matveev, 2021-02-28
@ematveev

push ax ; 2 раз
 mov ax,0B800h 
 mov es,ax 
 mov di,0 
 mov cx,2000 
 pop ax

here the ax register is used temporarily, to load into the values ​​in the es register

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question