News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

trying 32-bit in dos help

Started by www.:).com, December 01, 2010, 02:53:59 AM

Previous topic - Next topic

MichaelW

Dave,

A 32-bit register can be used as a base or index register in 16-bit code no problem, it just won't work the other way around.

In 16-bit code using 32-bit registers for indirect memory operands provides some significant advantages.

;==============================================================================
.model small, c
.386
include support.asm
.stack
;==============================================================================
.data
    myArray dw 0,1,2,3,4,5,6,7
.code
;==============================================================================
.startup
;==============================================================================
    ;---------------------------------------------------------------------
    ; For indirect memory operands with 16-bit registers, the operand can
    ; include a base register (BX or BP) or an index register (SI or DI),
    ; or one of each, and one or more displacements.
    ;
    ; Here the element offset is encoded as a displacement.
    ;---------------------------------------------------------------------

    mov bx, OFFSET myArray
  @@:
    mov ax, [bx+8]                    ; load element 4 into AX
    print word$(ax),13,10

    ;--------------------------------------------
    ; Here myArray and 8 are both displacements.
    ;--------------------------------------------

    xor bx, bx
  @@:
    mov ax, myArray[bx+8]             ; load element 4 into AX
    print word$(ax),13,10

    ;------------------------------------------
    ; Here the element offset is stored in SI.
    ;------------------------------------------

    mov bx, OFFSET myArray
    mov si, 8
  @@:
    mov ax, [bx+si]                   ; load element 4 into AX
    print word$(ax),13,10

    ;------------------------------------------------------------------
    ; To use the index (or base) register to index anything other than
    ; a BYTE array, you must scale the index by the size of the array
    ; elements in a separate instruction, before it is used to address
    ; the array.
    ;------------------------------------------------------------------

    mov si, 4                         ; load element index
    shl si, 1                         ; scale the index by 2
    mov ax, myArray[si]               ; load element 4 into AX
    print word$(ax),13,10

    ;---------------------------------------------------------------------
    ; For indirect memory operands with 32-bit registers, any general-
    ; purpose register can be used as the base register and any general-
    ; purpose register other than ESP as the index register. The operand
    ; can include a scale factor (1, 2, 4, or 8) on the index register,
    ; and one or more displacements.
    ;---------------------------------------------------------------------

    mov edx, OFFSET myArray
    mov ecx, 3
    mov ax, [edx+ecx*2+2]             ; load element 4 into AX
    print word$(ax),13,10

    mov ecx, 4
    mov ax, myArray[ecx*2]            ; load element 4 into AX
    print word$(ax),13,10

    call waitkey

;==============================================================================
.exit
end


eschew obfuscation

dedndave

thanks for correcting me Michael   :U
still - the offset value may not be larger than FFFFh, right ?
or does it wrap

www.:).com

Ok, so is this how 32-bit code woks or it this jut using 32-bit registers with a smaller bus(16-bit)? If I am correct wouldn't that make the code slower because it requires two clock cycles to store a register's data to memory rather than 1 cycle? Also I tried using the register ees, but got an error is there no such thing as ees?
- I was also wondering why I can only use the di register for a base register and not others, if I try to use another it gets an error, at least last I checked.

dedndave

segment registers only come in 16-bit size   :bg
in 32-bit protected mode programs, they do not directly become part of the address calculation like they do in 16-bit
instead, they are 16-bit indices into a table that describes the memory pages to be used

www.:).com

If segment regesters only come in 16 bit size then how come we can use 32 bit ones, :dazzled: Im confused about all of this 32 bit in 16 bit stuff.

redskull

Basically, you just get bigger registers, not a bigger address space.  Trying to specify an offset greater then that 20-bits you get will toss an exception.  And yes, you are (more-or-less) correct that moving a 32-bit value from a EnX register into a memory location (within the lower 1MB, of course), will take two shots.  Of course, it's all emulated under Windows anyhow, so it won't really work like that, but it might if you were using an antique computer

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

www.:).com

So if i were to get a memory location that is larger than 16 bits I would not be able to get any farther to that address with a 32 bit register than I would with a 16 bit register? From what I understand no matter what you can't access any more memory with 32 bit register than with 16 bit ones.
-If this is true then how do modern machines get large memory addresses - or is what we are doing not true 32 bit - aren't larger buses able to allocate larger memory locations.
Assuming true 32 bit is a CPU with a 32 bit bus and 32 bit registers.

redskull

Quote from: www.:).com on December 02, 2010, 02:08:57 AM
So if i were to get a memory location that is larger than 16 bits I would not be able to get any farther to that address with a 32 bit register than I would with a 16 bit register? From what I understand no matter what you can't access any more memory with 32 bit register than with 16 bit ones.

This is true when you are executing real-mode DOS code, but when writing code for protected-mode Winodws you get the full range.  But really, where did you plan on "getting" a memory location larger than 16-bits from in a DOS program anyway?

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

www.:).com

I don't know, just wondering - not like I need to I guess.

GregL

Quote from: www.:).com- or is what we are doing not true 32 bit -

Right, it's not true 32-bit, you are assembling 16-bit DOS code and using 32-bit registers. I would recommend you forget about 16-bit DOS code and learn MASM for 32-bit Windows.  And then there's 64-bit Windows too. :bg