Solved

Got Stuck with linking 16-bit assembly to 32-bit VC++ program

Posted on 2002-07-25
21
453 Views
Last Modified: 2007-12-19
Hi all,
  I have got stuck with an assembly program, which is written for port reading from an add-on ISA card. I need to write a VC++ application, which will get the values read by assembly program and do some manipulation. But I am unable to use the program in Windows.
  Can anyone show me a way as to how shall I proceed? I am really clueless on linking this assembly code with the VC++ application. Please help me.

Thanks in advance for any help.
San
"Be Good, Do Good".
0
Comment
Question by:san_hbs
  • 9
  • 7
  • 2
  • +1
21 Comments
 
LVL 9

Assisted Solution

by:BeyondWu
BeyondWu earned 100 total points
ID: 7178956
Impossible!
You can't read a port directly in a 32bit windows program. The "in/out" intel-opcodes is a privilege instruction under windows protect mode.
You need to write a driver or use a third party I/O library.
Here are a free direct I/O library.
http://www.ddj.com/ftp/1996/1996.05/directio.zip
0
 

Author Comment

by:san_hbs
ID: 7179381
BeyondWu,
>You can't read a port directly in a 32bit windows program. The "in/out" intel-opcodes is a privilege instruction
Can't I write 32-bit assembly code for port I/O?

Thanks a lot for your suggestions and help.
San
"Be Good, Do Good".
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 7184325
>>Can't I write 32-bit assembly code for port I/O?
Yes, you can't!
It has nothing to do with which tool(MASM/VC/VB..) or which language(ASM/C/C++..) you used.
0
 

Author Comment

by:san_hbs
ID: 7185402
Can the assembly program I am using work on Win98?
Can't I write assembly code for port I/O using Win98 OS?

Regards
San
"Be Good, Do Good".
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 7186767
>>Can the assembly program I am using work on Win98?
It depend on the type of the program and which port it access.
Do you know the type? Dos program(MZ)? 16bit Windows program(NE)? or 32bit windows program(PE)?

>>Can't I write assembly code for port I/O using Win98 OS?
Yes, except you write a VXD/WDM driver.
0
 

Author Comment

by:san_hbs
ID: 7187039
>>Do you know the type? Dos program(MZ)? 16bit Windows >>program(NE)? or 32bit windows program(PE)?
No, I don't.
The port I am willing to use is of an add-on ISA card. This port will receive data from a GPS (Global Positioning System) device. I am supposed to get this data and write it on a map image on the screen. This is a part of academic project. I have the ISA card, but not any driver. Please suggest a way to proceed.
Regards
San
"Be Good, Do Good".
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 7189441
>>I have the ISA card, but not any driver.
Can this card work normally on your PC without any driver?
If not, you need the spec of the card, and write a driver, it's complex:O(, you should work with the hardware engineer.

Otherwise, you only need to know which port it uses, and read the port simply with a driver(actually, any ring0 code).
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 7190359
Sorry san_hbs, I made a mistake.

You can read/write port directly under win98.
you can simply use inline _asm directive.
0
 

Author Comment

by:san_hbs
ID: 7190678
Hi BeyonwWu,
Here I am giving the complete code in Assembly, that is working in DOS.
==
     TITLE   IOC_OA
     DOSSEG
     .MODEL small

PUBLIC  _vectloc
PUBLIC  _oldvect
PUBLIC  _mask1
PUBLIC  _mask2

.data
_vectloc     DD     000001ccH
_oldvect     DD      0f000ff23H
EXTRN     _value:word
EXTRN     _index:word
_mask1 db 0
_mask2 db 0

.code

PUBLIC     _install
_install     PROC NEAR

push ax
push bx
push dx
push ds
push es
cli
mov al,99h
mov dx,323h
out dx,al
     les     bx,DWORD PTR _vectloc          
     mov     ax,WORD PTR es:[bx]          
     mov     dx,WORD PTR es:[bx+2]          
     mov     WORD PTR _oldvect,ax          
     mov     WORD PTR _oldvect+2,dx
     mov     WORD PTR es:[bx],OFFSET _handler
     mov     WORD PTR es:[bx+2],SEG _handler

in al,21h
mov _mask1,al
and al,0fbh
out 21h,al
in al,0a1h
mov _mask2,al
and al,0f7h
out 0a1h,al                              
mov al,10100110b
mov dx,321h
out dx,al
sti
mov al,020h
out 020H,al
out 0a0H,al

pop es
pop ds
pop dx
pop bx
pop ax
ret
_install     ENDP

PUBLIC     _trmnate
_trmnate     PROC NEAR
push ax
push bx
push dx
push ds
push es
cli
     les     bx,DWORD PTR _vectloc
     mov     ax,WORD PTR _oldvect
     mov     dx,WORD PTR _oldvect+2
     mov     WORD PTR es:[bx],ax
     mov     WORD PTR es:[bx+2],dx
mov al,_mask1
out 21h,al
mov al,_mask2
out 0a1h,al
sti
mov al,020h
out 020H,al
out 0a0H,al
pop es
pop ds
pop dx
pop bx
pop ax
ret
_trmnate     ENDP

PUBLIC     _handler
_handler     PROC FAR
pushf
push ax
push bx
push dx
push ds
mov ax,@data
mov ds,ax
mov dx,0320h
in al,dx
mov ah,al
add dx,2
in al,dx
mov bx,_index
mov word ptr _value[bx],ax
add _index,2
and _index,16383
mov al,020H
out 020H,al
out 0a0H,al
pop ds
pop dx
pop bx
pop ax
popf
sti
iret
_handler     ENDP

END
==

Can you please suggest me the enhancement I should make, so that it may work on win98??
Please suggest a way..
Thanks a lot for your concern to this problem
Regards
San
"Be Good, Do Good".
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Accepted Solution

by:
BeyondWu earned 100 total points
ID: 7192484
You should write a driver:O(, because of you should handle the interrupt.
I don't know if the interrupt vector(73H(1CCH/4)) is fixed by hardware, it maybe will be mapped to another vector in Win98.

Let me modify it, but I can't test it for you because I havn't the hardware:O(
You should familiar with WDM:O).

Because the interrupt handler seems very simple and fast, so I will not use a DPC.

//interrupt handler, just like the _handler of above code.
BOOLEAN _handler(PKINTERRUPT Interrupt,PVOID Context)
{
     // here,I think you can simply use the same code as
     // _handler
     // BUT,YOU SHOULD CHECK THE STATUS PORT OR REGISTER
     // OF THE CARD TO CHECK IF THE INTERRUPT IS GENERATED
     // BY THE CARD, IF NOT GENERATED BY YOUR CARD, YOU
     // SHOULD RETURN FALSE.
     _asm
     {
        pushf
        push ax
        // ....
     }
     return TRUE;
}

PKINTERRUPT  InterruptObject;

NTSTATUS DriverEntry( IN PDRIVER_OBJECT  DriverObject, IN PUNICODE_STRING RegistryPath )
{
     int irq = ??; //(0-15)can you find the card in win98?
     // the irq is the interrupt request of the hardware.
     // you can find it in "device manager" of windows.

     KIRQL irql = irq;
     int vector = 0x73;//just test!if the hardware mapped the same interrupt vector.
     ULONG MappedVector;
     // ...
     MappedVector = HalGetInterruptVector(Isa, 0, irq, irq,
               &irql, &Affinity);

     IoConnectInterrupt(&InterruptObject, _handler,
                        DeviceObject,
                        NULL,
                        MappedVector,
                        irql,
                        irql,
                        Latched,
                        FALSE,
                        Affinity,
                        FALSE
               );
    // ...
}
0
 

Author Comment

by:san_hbs
ID: 7205861
BeyondWu,
  I tried with the driver code you suggested, but I need to work on that for some time because I am really unaware of that.
  Here is another problem, which I am getting during runtime of the assembly code in Win98.
The following line of code give unhanded exception - Access Violation
          mov ax,WORD PTR es:[ebx]    
  Please suggest me a way
Regards
San
"Be Good, Do Good".
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 7207296
Why still use mov ax,WORD PTR es:[ebx]?
It's only available under 16bit realmode.
0
 

Author Comment

by:san_hbs
ID: 7208602
>Why still use mov ax,WORD PTR es:[ebx]?
Because I want to do it in Win98. Apart from the driver ,which may take long time, I want to use the assembly code also.
Thank you
Regards
San
"Be Good, Do Good".
0
 
LVL 3

Expert Comment

by:Dancie
ID: 7357942
>Answer to why use mov ax,word ptr es:[ebx]
First you can use word ptr in 32 bit code,this gets
and override byte before the instruction of 0x67. Sometimes
you must read only 2 bytes at a time, as in harddisk interfaceing.
Second - under windows segment registers ds ,es ,ss are always the same segment(selector), fs has another value which I do not know what it is for and gs is always 0.
You can drop the es: in the instruction, this costs about
one clock cycle extra and does not change anything.
Your Access Violation was caused by a bad pointer [ebx].
I have used with success the library "winio" for port access in win95 - windows2000. It is easy use to in C and assembler and I think shareware.
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 7358483
Dancie,
Of cource, you can use the instruction mov ax,word ptr es:[ebx], and everything you said are right, but san_hbs want to hook/change the IVT under win32! Not the I/O port, do you think it's possible to change the IVT in win2k with the "winio" driver?
0
 
LVL 3

Expert Comment

by:Dancie
ID: 7402029
Comment for BeyondWu
No winio cannot change the IVT (in protected mode it is called the Interrupt Descriptor table. The only way is to write a driver that runs in ring 0. There you can get the selector of the interrupt descriptor table with the instruction sidt[address]. After you have this you can make a data descriptor which mirrors the IDT and change it.
But there is actually much more to do than simply changing the data. You must know many other factors. It is possible but difficult.
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 7403883
For Dancie,
Agree with you, the normal approach is use IoConnectInterrupt, but for some IRQ it's not available, we only can with SIDT/LIDT.
It seems san_hbs has forgotten the question. We don't waste time for it.:O(
0
 

Author Comment

by:san_hbs
ID: 7405017
BeyondWu and Dancie,
I am extremely sorry for not being involved in your discussion for so long. Actually, fast few days have not been very nice for me. Anyway I am back and hope to continure it.
I tried to build the driver as suggested by You, but I am unable to test it properly. I am not even sure that the driver I have made is correct or not (Any tools to check it?). Now the platform of development has also changed. I am being asked to develop the driver on Win2k Platform. As per my understanding of the topic, if I develop a driver, it is supposed to work on Win98/2k platform. What I want is, the driver will take the data from the ISA card and store it into a buffer. This buffer will be accessed by another part of front-end application at the same time. Your suggestions are most welcome.
I am really greatful to you both for taking serious note of my problem and trying to help me.
Regards
San
"Be Good, Do Good".
0
 
LVL 3

Expert Comment

by:y2ksw
ID: 7891075
Although it is not recommmended, you *can* read and write directly to ports with Win95/98/ME, but not with NT/2000/XP.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Assembly Language - 8086 Processor 3 1,061
ASM problem 2 31 342
Cascading assemblies in .net libraries 4 352
Automating interactive programs 5 415
Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
Moving applications to the cloud or switching services to cloud-based ones, is a stressful job.  Here's how you can make it easier.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now