Board index » delphi » Registers in TP

Registers in TP

Could someone explain how the registers AX, BX, and DX
are used in TP?  I understand from the manual that these
registers can be freely modified.  However, I do not
understand what the registers are for.  If I am trying
to read and write values to a port which registers do
I use.  

I have a module that has a read register and write register
for each port.  Which registers do I use for this?

Any help would be greatly appreciated.

Regards,

Jeff
UCF1...@aol.com

 

Re:Registers in TP


In article <1998060311423101.HAA20...@ladder03.news.aol.com>,

Quote
UCF1566 <ucf1...@aol.com> wrote:
>Could someone explain how the registers AX, BX, and DX
>are used in TP?  I understand from the manual that these
>registers can be freely modified.  However, I do not
>understand what the registers are for.  If I am trying
>to read and write values to a port which registers do
>I use.  

Registers are memory locations internal to the processor. The 80x86 has
8 more or less general purpose 16-bit registers: AX BX CX DX SI DI BP SP.
If you do not know about what registers are then why do you need to read
or write them?

Quote

>I have a module that has a read register and write register
>for each port.  Which registers do I use for this?

Pardon? Where have you gotten that "module". What ports are you taking
about etc etc etc etc. Your question is simply way too ambiguous.

Osmo

Re:Registers in TP


Quote
UCF1566 wrote in message

<1998060311423101.HAA20...@ladder03.news.aol.com>...

Quote
> [snip question about writing CPU registers to port. Or something.]

>I have a module that has a read register and write register
>for each port.  Which registers do I use for this?

Jeff

I think you are suffering from some confusion here. The AX, BX etc.
registers are internal to the CPU. The registers on your ADC module
are internal to a chip on that module. How they are accessed depends
on the design of your ADC module. From your previous post it looks
like they are connected to the CPU's I/O ports. You will set or read
them by writing to or reading from these ports, which in Pascal means
assignments to or from the Port[] or PortW[] arrays (pseudo-arrays
really, because they don't live in memory). You DON'T need to use the
CPU registers unless you are writing assembler. Since you want to
program the module with Pascal you should almost certainly NOT use
them.

FP

Re:Registers in TP


Quote
UCF1566 wrote:

> Could someone explain how the registers AX, BX, and DX
> are used in TP?  I understand from the manual that these
> registers can be freely modified.  However, I do not
> understand what the registers are for.  If I am trying
> to read and write values to a port which registers do
> I use.

> I have a module that has a read register and write register
> for each port.  Which registers do I use for this?
> Any help would be greatly appreciated.
> Regards,
> Jeff UCF1...@aol.com

It must be noted that most I/O control chips name their
port addresses also as "registers". This is correct for
people who think in hardware terms. Several I/O related
chips have a couple of registers which can be read and
written with I/O commands. Some chips have one address
to select the "register number" and another port to read
or write into the addressed "register". This must not be
confused with CPU registers AL,AH,BL,BH etc.

Reading an I/O port
in ASM:   MOV    DX,port_number
          IN     AL,DX
in Pascal:   bb := Port[port_number]; where bb is a byte var

Writing an I/O port
in ASM:   MOV    DX,port_number
          OUT    DX,AL
in Pascal: Port[port_number] := bb;

It is very unusual to write words to I/O ports, since the
vast majority of I/O chips are byte oriented.

Franz Glaser
http://members.eunet.at/meg-glaser

Re:Registers in TP


Not having spent much time studying the actual schematics of a PC I can't
say where they actually are but for the purposes of programming its best to
treat them as though they are actually inside the CPU.

What are they ?

variables  they can hold bytes and words and segments(seemingly)

Why use them?

Speed.  

How can I use them ?

You can get at them from Pascal but it is simply
easier to use assembler.

Where do I use them?

Well, anytime you uses assembler I suppose but
they are useful in loops using a register as a loop
counter is about a trillion times faster than using
a memory operand
( I'll be posting some routines in a couple of minutes so look there)

--
Said the spider, "My pretty little tasty thing, you think you will go to
hell?  Whatever makes you hink your alive?"

Osmo Ronkanen <ronka...@cc.helsinki.fi> wrote in article
<6l3h5d$...@kruuna.Helsinki.FI>...

Quote
> In article <1998060311423101.HAA20...@ladder03.news.aol.com>,
> UCF1566 <ucf1...@aol.com> wrote:
> >Could someone explain how the registers AX, BX, and DX
> >are used in TP?  I understand from the manual that these
> >registers can be freely modified.  However, I do not
> >understand what the registers are for.  If I am trying
> >to read and write values to a port which registers do
> >I use.  

> Registers are memory locations internal to the processor. The 80x86 has
> 8 more or less general purpose 16-bit registers: AX BX CX DX SI DI BP SP.
> If you do not know about what registers are then why do you need to read
> or write them?

> >I have a module that has a read register and write register
> >for each port.  Which registers do I use for this?

> Pardon? Where have you gotten that "module". What ports are you taking
> about etc etc etc etc. Your question is simply way too ambiguous.

> Osmo

Re:Registers in TP


In article <01bd55f6$063fc440$0100007f@daylieen>,

Quote
Daylieen Theyalas <l...@coventry.ac.uk> wrote:
>Not having spent much time studying the actual schematics of a PC I can't
>say where they actually are but for the purposes of programming its best to
>treat them as though they are actually inside the CPU.

Of course they are inside the CPU.

Quote

>What are they ?

>variables  they can hold bytes and words and segments(seemingly)

Yes, one can consider them variables that are inside the CPU.

Quote

>Why use them?

>Speed.  

This is somewhat confusing. In Assembler one has to use them. period.
Sure one can choose between storing much of the information in registers
and choosing to store all in memory and using registers only as
temporary storage.

In High level languages the above choice is made by the person who
writes the compiler. That is why they are called high level languages.
Turbo Pascal uses the second option: all variables are stored in memory.
Some languages, like Turbo C use also registers to store local
variables. The C language even has a way where the user can tell the
compiler to store a particular variable in register (if possible).

Here is an example.  Lets have same code in Pascal and C and the compare
what TP and TC produces:

function pow(x:integer;y:word):integer;
var i,s:integer;
begin
  s:=1;
  for i:=1 to y do s:=s*x;
  pow:=s;
End;

begin
  writeln(pow(2,10));
End.

int pow(int x,unsigned int y)
{
        register unsigned int i,s;

        s=1;
        for (i=1;i<=y;++i) s*=x;
        return(s);

Quote
}

main()
{
   printf("%d\n",pow(2,10));

Quote
}

The Pascal code produces (for the function pow):

PROGRAM.POW: begin
  cs:0000 C8080000       enter  0008,00
PROGRAM.6:  s:=1;
  cs:0004 C746FA0100     mov    word ptr [bp-06],0001
PROGRAM.7:  for i:=1 to y do s:=s*x;
  cs:0009 8B4604         mov    ax,[bp+04]
  cs:000C 8946F8         mov    [bp-08],ax
  cs:000F B80100         mov    ax,0001
  cs:0012 3B46F8         cmp    ax,[bp-08]
  cs:0015 7F19           jg     PROGRAM.8 (0030)
  cs:0017 8946FC         mov    [bp-04],ax
  cs:001A EB03           jmp    001F

  cs:001C FF46FC         inc    word ptr [bp-04]
  cs:001F 8B46FA         mov    ax,[bp-06]
  cs:0022 F76606         mul    word ptr [bp+06]
  cs:0025 8946FA         mov    [bp-06],ax
  cs:0028 8B46FC         mov    ax,[bp-04]
  cs:002B 3B46F8         cmp    ax,[bp-08]
  cs:002E 75EC           jne    001C
PROGRAM.8:  pow:=s;
  cs:0030 8B46FA         mov    ax,[bp-06]
  cs:0033 8946FE         mov    [bp-02],ax
PROGRAM.9: End;
  cs:0036 8B46FE         mov    ax,[bp-02]
  cs:0039 C9             leave
  cs:003A C20400         ret    0004

Note how every variable is in the memory and there are 4 memory loads
and 2 memory writes inside the loop. The code is so poor that no
assembler programmer would write anything similar. It basically uses
only AX register (the mul uses also DX but that is purely a side-effect
here)

Now compare this to the code that TC++ 3.0 produces:

_pow: int pow(int x,unsigned int y)
  cs:0291 55             push   bp
  cs:0292 8BEC           mov    bp,sp
  cs:0294 56             push   si
  cs:0295 57             push   di
  cs:0296 8B5E04         mov    bx,[bp+04]
  cs:0299 8B4E06         mov    cx,[bp+06]
#KOE#7:  s=1;
  cs:029C BF0100         mov    di,0001
#KOE#8:  for (i=1;i<=y;++i) s*=x;
  cs:029F BE0100         mov    si,0001
  cs:02A2 EB07           jmp    02AB

  cs:02A4 8BC7           mov    ax,di
  cs:02A6 F7EB           imul   bx
  cs:02A8 8BF8           mov    di,ax
  cs:02AA 46             inc    si
  cs:02AB 3BF1           cmp    si,cx
  cs:02AD 76F5           jbe    02A4
#KOE#9:  return(s);
  cs:02AF 8BC7           mov    ax,di
  cs:02B1 EB00           jmp    #KOE#10 (02B3)
#KOE#10: }
  cs:02B3 5F             pop    di
  cs:02B4 5E             pop    si
  cs:02B5 5D             pop    bp
  cs:02B6 C3             ret

Notice how not only are the variables i and s in registers (SI and DI
respectively) but also the parameters x and y are loaded to registers
(BX and CX respectively). That code is IMO very good the only real minus
IMO is the unnecessary jump to the next instruction. (This has no
practical influence though.

Note that TC is not among the best code generators when one considers C
compilers. Good code generation is not, however, bound to C. One could
make a Pascal compiler that could produce equally fast coded. (though
some features, like how the function return value is specified help to
make fast code). For some reason Borland decided to go different rout
then it made TP. If chose slow code but fast compiling.

The bonus with the TP approach is that when you write assembler routines
you need to save fewer registers. In TC you apparently need to save at
least SI and DI.

Quote

>How can I use them ?

>You can get at them from Pascal but it is simply
>easier to use assembler.

>Where do I use them?

>Well, anytime you uses assembler I suppose but
>they are useful in loops using a register as a loop
>counter is about a trillion times faster than using
>a memory operand

Actually not a trillion. Maybe 2-3 times faster.

There is no reason explicit use registers in HLL unless one wants
speed with ASM or one interfaces to low level features like interrupts.
The second can be done in Pascal by using registers type defined in DOS
unit. For example:

var rg:registers;
....

fillchar(rg,sizeof(rg),0);  {make sure all unneeded registers are zeroes }
rg.ah:=xxx;
rg.dx;=zzz;
intr(intrnro,rg);
if rg.flags and fcarry >0 then Writeln('Carry set');

Osmo

Re:Registers in TP


Quote
Osmo Ronkanen (ronka...@cc.helsinki.fi) wrote:

: Daylieen Theyalas <l...@coventry.ac.uk> wrote:
: >What are they ?
: >
: >variables  they can hold bytes and words and segments(seemingly)
:
: Yes, one can consider them variables that are inside the CPU.

       The idea that CPU registers can hold "segments"
should be refuted, I think ax and bx registers are
word registers, and eax-ebx registers are double word.
       The ax-bx size registers can hold the "segment
_ADDRESS_" only, and the use of the [value] brackets
specifies that it is the _content_ of that address
that is to be manipulated.
       It is difficult for some beginners to understand
the difference between an address $4000, and the content
of that address [$4000] (I think? ). :-)

: >Why use them?
: >
: >Speed.  
:
: This is somewhat confusing. In Assembler one has to use them. period.
: Sure one can choose between storing much of the information in registers
: and choosing to store all in memory and using registers only as
: temporary storage.

      The rest of your presentation, with examples,
seems to assume that the number of bytes of machine
code is directly related to speed, where speed means
time required to execute.
      This would be true if each instruction used
the same number of CPU cycles, and I don't think
they do.   As I recall, some instructions only
use one or two cycles, and some can use up to eight
or more cycles, and I don't think it matters how
many bytes the machine code carrying the instruction
uses, although that does affect speed also, I suppose.
      Of course CPU cycles are only given in CPU
books, and with present day memory size and CPU
speeds, should possibly even need to be considered.

Regards,

Ken Fischer

---

Re:Registers in TP


Quote
In article <EutDFI....@iglou.com>, Ken Fischer <kefis...@iglou.com> wrote:
>Osmo Ronkanen (ronka...@cc.helsinki.fi) wrote:
...
>:
>: This is somewhat confusing. In Assembler one has to use them. period.
>: Sure one can choose between storing much of the information in registers
>: and choosing to store all in memory and using registers only as
>: temporary storage.

>      The rest of your presentation, with examples,
>seems to assume that the number of bytes of machine
>code is directly related to speed, where speed means
>time required to execute.

It would be nice if you did not cut just the part you are replying to.

Quote
>      This would be true if each instruction used
>the same number of CPU cycles, and I don't think
>they do.   As I recall, some instructions only
>use one or two cycles, and some can use up to eight
>or more cycles, and I don't think it matters how
>many bytes the machine code carrying the instruction
>uses, although that does affect speed also, I suppose.

Could make your mind? The fact is that longer instructions generally are
slower as they take longer to fetch. Of course newer processors are fast
at fetching so that is not a problem but with something like 8088 the
fetching time was what dominated and it is not mentioned in the
execution times that you read at books.

Using memory is also slower as one has to put more instructions and
the instructions are slower. In addition the memory access prevents
the processor from fetching new instructions for execution.

Osmo

Other Threads