tags:

views:

124

answers:

3

function 09h interrupts 21h dx = offset of the text , ds = segment of the text

how can i obtain segment and offset in c++?

+1  A: 

If you're in real mode, they're the upper and lower 16-bits (respectively) of a FAR pointer to the data.

Environments that use real-mode pointers and let you directly call software interrupts are really rare these days. On any modern OS, you'd be using a user-mode wrapper which generates a sysenter instruction rather than int.

Ben Voigt
reason for downvote?
Ben Voigt
+3  A: 

There are a few prerequisites to this answer:

  1. You are using a compiler that generate 16 bit code since DOS calls only work in 16 bit mode and
  2. Your compiler gets the concept of segments.

In which case it's:

char string [] = "Hello World$";
call_int21h_9 (&string);

where call_int21h_9 is something like:

call_int21h_9 (FAR char *string)
// the FAR above is important and very compiler dependent, it tells the compiler
// that the pointer is a 32bit pointer (16 bit segment and 16 bit offset)
{
  mov ah,9
  lds dx,string ; this loads the DS:DX segment:offset from the value on the stack
  int 21h
}

Further to this, there are several ways to compile a 16 bit application depending on how the segments are set up and used. The two most common are small and large (the compiler might call them something else):

  • Small: All segments are the same value and the total of data + code is < 64k
  • Large: All code and data exist in many segments and code + data < 1Meg

There are other layouts of segments (one data, many code; one code and many data, etc) and you'll need to consult the compiler documentation to see what's available.

Of course, your biggest problem is getting a compiler that does 16 bit.

Why not just use cout or printf?

Skizz
cuz , i should convert this code from pascal to c++Uses dos;var r : registers; text : string[128];begin text :='Hello$'; r.ah := $09; r.dx := ofs(text)+1; r.ds := seg(text); intr($21,r);end.
RealBoy_Ba
Do you need to convert that to _modern_ C++ or _equally-obsolete_ C++ ?
MSalters
equally-obsolete C++
RealBoy_Ba
A: 
#include <dos.h>

FP_SEG(&var);

returns segment of var

FP_OFF(&var);

returns offset of var

RealBoy_Ba