• Please review our updated Terms and Rules here

Borland Turbo C BASM / x86 question

alank2

Veteran Member
Joined
Aug 3, 2016
Messages
2,264
Location
USA
I am trying to figure out BASM so I am making a test function, in this case a simple DOS_Open function.

I've worked out how to make it work with different memory models, but the one thing I'd like to see how to do is how to get AX into the AHandle pointer. In this case my method is using mov to copy ax into handle, and then use a C command *AHandle=handle. Is there a better way to do this? I've tried multiple things, but it seems like I'm trying to assign ax to a pointer of a pointer maybe? What asm can I replace the two bold lines with?

Code:
int DOS_Open(char *AFilename, unsigned char AAccess, int *AHandle)
{
  int handle;

  #if defined(__COMPACT__) || defined(__LARGE__) || defined(__HUGE__)
    asm push ds
    asm lds dx, AFilename
  #else
    asm mov dx, AFilename
  #endif
  asm mov al, AAccess
  asm mov ah, 0x3d
  asm int 0x21
  #if defined(__COMPACT__) || defined(__LARGE__) || defined(__HUGE__)
    asm pop ds
  #endif
  asm jc fail
[b]  asm mov handle, ax
  *AHandle=handle;[/b]
  //asm mov AHandle, ax
  return 0;

  fail:
  return -1;
}
 
I saw your question earlier in the week and because I don't have a straight and easy answer I didn't bother writing a reply.

But... This question kept rolling around in my brain and even though I still don't have a straight and easy answer, I'll have a stab at writing some thoughts.

Can it be done? I am sure it can be done, but it's not straightforward. First of all, the *AHandle parameter needs to be treated different in different memory models as it can either be just a 16 bit offset into the current DATA segment or a 32bit segment/offset pair.

Let's assume for a moment that the memory model is TINY or SMALL where there is a single DATA segment pointed to by DS.

The stack is represented by SS:SP where BP is set to the "bottom" of the stack (i.e. SP - BP = size of stack). What you need to figure out is, what the offset of *AHandle is on the stack and use BP to dereference it. E.g. if the offset of *AHandle was 4 then something like "mov ax, [word ptr ss:bp + 4]" would load the value of *AHandle into the AX register.

Now *AHandle is a pointer, so it refers to a memory address, in our case (memory model TINY/SMALL) it is a 16 bit offset into the data segment (DS). So, to get the value of AX into the memory location represented by *AHandle, you first have to load stack value in e.g. SI and then you can set DS:SI to the value of AX.

Not sure if this makes sense though. I am sure there is someone much smarter around here who can explain it better, at least we have a discussion going :)
 
Back
Top