flat assembler
Message board for the users of flat assembler.

Index > DOS > Problem with ExitIfESC macro

Author
Thread Post new topic Reply to topic
Slai



Joined: 11 Jan 2006
Posts: 40
Location: NY/Bulgaria
Slai 22 Jan 2006, 19:36
Code:
macro ExitIfESC {
        push ax
        GetKey             ; mov  ah, 00h      int  16h
        cmp  al,1Bh        ; if ESC
   local _ExitIfESCjump
     jne  _ExitIfESCjump
        Text               ; mov  ax,0003h     int  10h
        ExitToDOS          ; mov  ax,4C00h     int  21h
   _ExitIfESCjump:
        pop ax }    

My program first get in VGA, and than uses this macro in the drawing loop, but when I press Esc, the program gets in text mode, fullscreen, and dosent close. What should I do to make it close ?

If you are confused from the macro, here is its code from Debug :

PUSH AX
MOV AH,00
INT 16
CMP AL,1B
JNZ 0113
MOV AX,0003
INT 10
MOV AX,4C00
INT 21
POP AX
Post 22 Jan 2006, 19:36
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 23 Jan 2006, 13:17
Slai wrote:
Code:
macro ExitIfESC {
        push ax
        GetKey             ; mov  ah, 00h      int  16h
        cmp  al,1Bh        ; if ESC
   local _ExitIfESCjump
     jne  _ExitIfESCjump
        Text               ; mov  ax,0003h     int  10h
        ExitToDOS          ; mov  ax,4C00h     int  21h
   _ExitIfESCjump:
        pop ax }    

My program first get in VGA, and than uses this macro in the drawing loop, but when I press Esc, the program gets in text mode, fullscreen, and dosent close. What should I do to make it close ?

If you are confused from the macro, here is its code from Debug :

PUSH AX
MOV AH,00
INT 16
CMP AL,1B
JNZ 0113
MOV AX,0003
INT 10
MOV AX,4C00
INT 21
POP AX


hi,
well this does not look good,
you pop back ax after you exit,
so what whould be your problem? the program gets into full screen mode?
it will surely get to fullscreen mode if you go graphical, on w98 and winxp too, you can use dosemu on windows, that handles graphic modes in a window.
Post 23 Jan 2006, 13:17
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 23 Jan 2006, 14:24
Actually this code is a proper way to do this and should correctly exit on ESC. If after setting text mode the command prompt (or whatever shell you run it from) doesn't come back, it may mean that you have corrupted some of the DOS memory with some other code, what caused DOS to hang after your program exited. Check out all the other code in your program for the possibility that it writes into some memory area that it shouldn't write to.
Post 23 Jan 2006, 14:24
View user's profile Send private message Visit poster's website Reply with quote
Slai



Joined: 11 Jan 2006
Posts: 40
Location: NY/Bulgaria
Slai 23 Jan 2006, 21:12
Yes, it appeared that there is nothing wrong with the code .. It was just because I ran the program with F9 from FASMW ..
When I ran the program in the Windows Explorer, everything was as it is suppose to be Smile

One more little question that is actualy for the Macroinstructions section, but it is related to the exiting from VGA to DOS.
As I know there is no difference between
int 20h, and mov ah,4Ch int 21h , exept that the one is returning exit code, right ?

so now I have a Exit macro like this:

Code:
macro Exit _ExitCode {
    if _ExitCode eq
        int  20h
    else
        mov  al,_ExitCode
        mov  ah,4Ch
        int  21h
    end if }    
and my question: is there a way to check in the macro if the program is in text mode, so if the program is in text mode, the Exit macro after preprocessing to become just int 20h , but if the program is not in text mode (VGA for example) , the Exit macro after preprocessing to become

mov ax,3
int 10h
int 20h

?

or I just must change the macro to :

Code:
macro Exit _ExitCode {
    if _ExitCode eq
        mov ax,3
        int 10h
        int  20h
    else
        mov ax,3
        int 10h
        mov  al,_ExitCode
        mov  ah,4Ch
        int  21h
    end if }    
?
Post 23 Jan 2006, 21:12
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 23 Jan 2006, 21:24
Function 0Fh checks for current video mode, so you can check for mode 3 like this:
Code:
mov ah,0Fh
int 10h
cmp al,3
je already_in_text_mode    


Also it's not true that there's no difference between int 20h and function 4Ch of int 21h other than exit code - the int 20h requires CS to be the segment with PSP (which is true in .com files, but in case of .exe it can be true only when they are set up in that special way), while int 21h/function 4Ch can be called from anywhere.
Post 23 Jan 2006, 21:24
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 23 Jan 2006, 22:00
And IMO for this shouldn't be used macro, but rather some procedure
Post 23 Jan 2006, 22:00
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Slai



Joined: 11 Jan 2006
Posts: 40
Location: NY/Bulgaria
Slai 23 Jan 2006, 22:49
Yes, I was almost sure that this check can not be done with macroinstructions, but just wanted to make sure.
Thanks Smile For now on I will use int 20h only in the .com files for security
Post 23 Jan 2006, 22:49
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 23 Jan 2006, 23:44
it CAN be done, but it seems you don't understand what macroinstructions are. (or what they are for)

They do only textual substition before compiling your code, so writing:
Code:
macro a
{x
 y z}
mov eax,5
a
mov eax,6
a
a    

will be just same as if you would write
Code:
mov eax,5
x
y z
mov eax,6
x
y z
x
y z    


They aren't good for often-used routines, because then you will end up having same routine 50 times in code. It is better to have it once in code, as procedure, and then just call it.
Post 23 Jan 2006, 23:44
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Slai



Joined: 11 Jan 2006
Posts: 40
Location: NY/Bulgaria
Slai 24 Jan 2006, 01:47
I know that, but I prefer optimizing for speed than optimising for size .. especially in graphics and game programs ..
Although, if I have to repeat the same code more than 10-20 times, I will probably use a procedure.
Post 24 Jan 2006, 01:47
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 24 Jan 2006, 02:23
well, usually only small "innermost" (in code hierarchy) parts of code need to be optimized, eg. those which are executed most of time, and these usually don't have exit-if-ESC-pressed check Wink But it's allright if you know what you are doing.

Also not that this decreases readability of your code, because if someone else (or you after some time) look at the code, you don't know what all the macros really do and you have to lookup their definition or quess (possibly wrong). Maybe it looks better for you know, but in big long-term project this isn't very good idea.
Post 24 Jan 2006, 02:23
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 24 Jan 2006, 08:34
Plus int 21h, 4Ch closes any open files.
Tomasz Grysztar wrote:
Also it's not true that there's no difference between int 20h and function 4Ch of int 21h other than exit code - the int 20h requires CS to be the segment with PSP (which is true in .com files, but in case of .exe it can be true only when they are set up in that special way), while int 21h/function 4Ch can be called from anywhere.
Post 24 Jan 2006, 08:34
View user's profile Send private message Visit poster's website Reply with quote
Slai



Joined: 11 Jan 2006
Posts: 40
Location: NY/Bulgaria
Slai 26 Jan 2006, 05:56
what about ret .. What is the risk if I exit the program with ret ?
Post 26 Jan 2006, 05:56
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 26 Jan 2006, 07:53
You can only exit from .com programs with ret.
This works because of the few facts combined:
1) The .com program is loaded into the same segment as its PSP, just after it (the size of PSP is 100h bytes), so the CS is the same as the PSP segment.
2) The first two bytes of PSP are 0CDh and 20h, which is an opcode of "int 20h" instruction.
3) The stack for .com program is set up on the top of the same segment, with one zero word (two zero bytes) initially pushed on it, so SS=CS(=DS=ES), SP=0FFFEh, and word [0FFFEh]=0.

Now when you execute a RET instruction, it takes the return address from the stack, and it is 0, so it goes to CS:0, and because CS is the PSP segment, it executes the "int 20h" and thus exits the program.
With .exe program this won't work this way, except for some very special cases.
Post 26 Jan 2006, 07:53
View user's profile Send private message Visit poster's website Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2 18 Dec 2006, 10:06
That's what I needed to know.

I only do COM files in DOS,so no worries there. I can save 1 byte of space!
Post 18 Dec 2006, 10:06
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.