Author Topic: Extracting 16-bit value without using a 32-bit register  (Read 17126 times)

Offline mik3ca

  • Jr. Member
  • *
  • Posts: 30
Extracting 16-bit value without using a 32-bit register
« on: January 31, 2021, 07:03:01 PM »
I'm trying to figure out how to work with 16-bit values in 32-bit spaces because some functions need to use the space as 32-bit but I only use 16-bit numbers.

In this code example, we assume I'm calling DOS interrupt 2Fh with function 0 (AX=0) and we assume function treats the internal 32-bit value aval as a size to a string (for example).

The values I want to use are all 16-bit so I want to be able to load a 16-bit value in and retrieve a 16-bit value from the same variable after the interrupt is executed.

The safest approach is with the code below:

Code: [Select]
myfunction:
    xor EAX,EAX
    mov AX,CX
    mov [CS:aval],EAX
    xor AX,AX
    int 2Fh
    mov EAX,[CS:aval]
    mov CX,AX
ret

aval dd 0h  ;32-bit storage space that an interrupt requires
                     ;because interrupt checks ALL 32 bits, not just part.

I thought of replacing the above with one of these fragments, but the endianness of the intel processors sometimes confuse me because in some instances, values are stored backwards.

Here's idea 1 of shorter code:

Code: [Select]
myfunction:
    mov [CS:aval],CX
    xor AX,AX
    int 2Fh
    mov CX,[CS:aval]
ret

aval dd 0h

Here's idea 2 of shorter code:

Code: [Select]
myfunction:
    mov [CS:aval+2],CX
    xor AX,AX
    int 2Fh
    mov CX,[CS:aval+2]
ret

aval dd 0h

So which of my 2 ideas would work best? or is there a better idea? This code fragment will be part of an MS-DOS COM file when compiled.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Extracting 16-bit value without using a 32-bit register
« Reply #1 on: January 31, 2021, 10:48:16 PM »
I'm not sure I understand what you're doing. Idea 1 should work... better,, maybe "at all". You might want to make the variable "dw" or "resw 1" (in .bss) and use "movsx" to get it unto a 32 bit register? What works?

Best,
Frank


Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Extracting 16-bit value without using a 32-bit register
« Reply #2 on: February 01, 2021, 12:00:12 AM »
Very strange to use MULTIPLEX syscall from int 0x2f this way. But, what is the problem using E?? registers in 16 bits environment? You're dealing with processors prior to 386?

Offline mik3ca

  • Jr. Member
  • *
  • Posts: 30
Re: Extracting 16-bit value without using a 32-bit register
« Reply #3 on: February 02, 2021, 12:35:22 AM »
Very strange to use MULTIPLEX syscall from int 0x2f this way.
The interrupt number is an example.

But, what is the problem using E??
It's fine for the most part, but I was aiming to save code space and prefixing registers with E often require at least 1-2 extra bytes per instruction.

registers in 16 bits environment?
You're dealing with processors prior to 386?

The target environment is MS-DOS.
I'm doing a special project where my own custom circuit can connect to the computer via RS232 serial port then connects to the internet via RJ45 network cable and MS-DOS packet driver.

I just think its an overkill to have windows on the target computer because I eventually will turn the computer into a special machine that constantly communicates with my hardware and if things break down (like power outages), I want the recovery to be fast, and loading into DOS is much faster than Windows. Also, the target computer is a DELL thinclient and I have a video adapter attached to it so I can't really be using graphic modes without distortion at best.