[comp.os.msdos.programmer] Writing assembly procedures for higher level languages

jhall@wpi.WPI.EDU (John Clinton Hall) (01/24/91)

AccordingtMicrosoft's _Mixed-Language Programming Guide_, "A procedure
caled from any of the Microsoft high-level languages should preserve the
values of SI, DI, SS, and DS (in additon to BP, which is already saved)." 
However, does one need to save the ES register if it is mdified?  How abo
the flags register?
-- 
The highest sounds are hardest to hear.  ## ## ## ##### ##  jhall@wpi.wpi.edu
Going forward is a way to retreat.       ## ## ## ## ## ##  Worcester
Great talent shows itself late in life.  ## ## ## ##### ##  Polytechnic
Even a perfect program still has bugs.   ######## ##    ##  Institute

ong@d.cs.okstate.edu (ONG ENG TENG) (01/25/91)

From article <1991Jan24.133054.8715@wpi.WPI.EDU>, by jhall@wpi.WPI.EDU (John Clinton Hall):
> AccordingtMicrosoft's _Mixed-Language Programming Guide_, "A procedure
> caled from any of the Microsoft high-level languages should preserve the
> values of SI, DI, SS, and DS (in additon to BP, which is already saved)." 
> However, does one need to save the ES register if it is mdified?  How abo
> the flags register?

I think the flags are "don't care", meaning return in indeterminate state.
You can always test it.  

Any how, I got a bone to pick with Microsoft C mixed-language
facility.  My department put down three hundred big ones for the 
compiler and interface seems not to work in a consistant way.  
When I finally got the C-main-program/Machine-assembler-subroutine to
work (both Microsoft) with a small size routine, I started to put a 
bigger assembler subroutine in (about 30k output code), but the
program failed, even after much debugging.  I have written 
nearly 1 meg of 8086 assembler (measured by output codes) with C 
as main program (Microsoft assembler, cheap $20 MIX C compiler), and
can always get them to work the way I want.  Can anyone comment
on working with C/Assembler interfacing with Microsoft C compiler and
Microsoft Assembler, and maybe speculate what I am doing wrong.  Also,
due to departmental decision, I have to use Turbo C and Turbo Assembler,
anyone's got any experience and thing to say?

Thanks in advance.

E. Teng Ong (ong@d.cs.okstate.edu) 

data@buhub (Mark Hall) (01/25/91)

jhall@wpi.WPI.EDU (John Clinton Hall) writes:

>AccordingtMicrosoft's _Mixed-Language Programming Guide_, "A procedure
>caled from any of the Microsoft high-level languages should preserve the
>values of SI, DI, SS, and DS (in additon to BP, which is already saved)." 
>However, does one need to save the ES register if it is mdified?  How abo
>the flags register?

To save the ES register, you can always:

	MOV AX,ES
	PUSH AX

	...

	POP AX
	MOV ES,AX

The general registers (AX - DX) can transfer stuff into the segment registers
and vice versa.  Just remember: if you save the REAL AX register, and then mov
like above, you'll have to pop the "fake" AX register before you pop the
"real" one.
--
| Internet: data@{bucs1,buhub}.bradley.edu, al632@cleveland.freenet.edu       |
|-----------------------------------------------------------------------------|
| F'net : 1:232/28 1:2250/9  (fname.lname@f<node>.n<net>.z<zone>.fidonet.org) |
|-----------------------------------------------------------------------------|
| "He sees you when you're sleeping, he knows when you're awake, but Captain, |
| it makes no sense!"    "But Spock, with Santa, ALL things are possible!"    |
| Kirk and Spock...Hallmark Greeting Cards                                    |

hpa@casbah.acns.nwu.edu (Peter Anvin) (01/25/91)

>>However, does one need to save the ES register if it is mdified?  How abo
>>the flags register?
You don't need to save ES in subroutines to Microsoft (or Borland)
high-level languages.  ES is a "scratch segment register", you can change
it at your heart's desire.  The ultra-transient flag register is never used
but right after it is set, so the same thing applies here.

NB: You *do* need to save ES if you are writing an interrupt handler ONLY.
FLAGS is saved by the interrupt itself, and restored by IRET, so you don't
need to save them here, either.

#ifndef SKIP_FLAMES
>To save the ES register, you can always:
>	MOV AX,ES
>	PUSH AX

>	POP AX
>	MOV ES,AX

Oh... I must be very old-fashioned to still use 'em old 8088
        PUSH ES
        POP ES

(no offense intended)
#endif
-- 
H. Peter Anvin +++ A Strange Stranger +++ N9ITP/SM4TKN +++
INTERNET:  hpa@casbah.acns.nwu.edu   FIDONET:  1:115/989.4
BITNET:    HPA@NUACC                 RBBSNET:  8:970/101.4

dmurdoch@watstat.waterloo.edu (Duncan Murdoch) (01/25/91)

In article <2916@casbah.acns.nwu.edu> hpa@casbah.acns.nwu.edu (Peter Anvin) writes:
>>>However, does one need to save the ES register if it is mdified?  How abo
>>>the flags register?
>You don't need to save ES in subroutines to Microsoft (or Borland)
>high-level languages.  ES is a "scratch segment register", you can change
>it at your heart's desire.  The ultra-transient flag register is never used
>but right after it is set, so the same thing applies here.

I think that MS Fortran depends on the flags staying unchanged in some
circumstances.  I'll see if I can dig up the version number where this is true,
and some demo code, because it's a bug, not a feature.

Duncan Murdoch

pipkinsj@cpqhou.uucp (Jeff Pipkins @Adv Dev@SE hou ) (01/25/91)

In article <1991Jan24.133054.8715@wpi.WPI.EDU> jhall@wpi.WPI.EDU (John Clinton Hall) writes:
>
>AccordingtMicrosoft's _Mixed-Language Programming Guide_, "A procedure
>caled from any of the Microsoft high-level languages should preserve the
>values of SI, DI, SS, and DS (in additon to BP, which is already saved)." 
>However, does one need to save the ES register if it is mdified?  How abo
>the flags register?

No, you don't have to save ES if it doesn't say that you have to save ES.
As for the flags, all of the code the compiler generates assumes that
the direction flag is 0 (CLD), and of course, interrupts should be enabled
(STI).  You don't want the single-step flag on either.  But the arithmetic
flags don't have to be saved (zero, carry, etc.).  If you modify BP you
will have to restore it before returning if you use the standard sequence.
The last thing your function does before returning is LEAVE (= mov sp, bp;
pop bp).

Good luck.

-- 
Jeff D. Pipkins (uunet!cpqhou!pipkinsj)

My disclaimer: My opinions do not necessarily reflect those of my employer.
Papaw's disclaimer: I've already told you more than I know.

risto@tuura.UUCP (Risto Lankinen) (01/29/91)

data@buhub (Mark Hall) writes:

>jhall@wpi.WPI.EDU (John Clinton Hall) writes:

>>AccordingtMicrosoft's _Mixed-Language Programming Guide_, "A procedure
>>caled from any of the Microsoft high-level languages should preserve the
>>values of SI, DI, SS, and DS (in additon to BP, which is already saved)." 
>>However, does one need to save the ES register if it is mdified?  How abo
>>the flags register?

>To save the ES register, you can always:

>	MOV AX,ES
>	PUSH AX

Hi!

Why not PUSH ES directly?

Anyway, I've had bad experience with saving the ES register in programming
in MASM for Windows.  I once thought it was a good idea to...

   PUSH DS
   MOV DS,AX ; This is part of the standard Windows call sequence
   PUSH SI
   PUSH ES
   PUSH DI   ; This order of PUSH:es facilitates LES and LDS at restore-time

Now, severe errors bugged me (literally) quite a while - until I discovered,
that in protected-mode environment the actual segment, whose selector ES
were, has a chance of being invalidated during subsequent calls to Windows'
kernel.  The errors were the result of the LES instruction (or POP ES for
that matter) at the end of the function loading the (at that time) invalid
selector 'back' to a segment register.

This is not a problem with the pushed DS , because there is a mechanism which
keeps the saved DS (and the CS of the far return) automatically up-to-date.

Terveisin: Risto Lankinen
-- 
Risto Lankinen / product specialist ***************************************
Nokia Data Systems, Technology Dept *  2                              2   *
THIS SPACE INTENTIONALLY LEFT BLANK * 2 -1 is PRIME!  Now working on 2 +1 *
replies: risto@yj.data.nokia.fi     ***************************************