ASM 'Out Instruction' crashes, code included.

This is a discussion on ASM 'Out Instruction' crashes, code included. within the C Programming forums, part of the General Programming Boards category; I am soon going to resume my study of IR using Parallel port etc.. So I decided to learn how ...

  1. #1
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    ASM 'Out Instruction' crashes, code included.

    I am soon going to resume my study of IR using Parallel port etc..
    So I decided to learn how to write data to the DATA bits 0-7. It works through the DOS debug, but not Assembly.

    Here is my code:
    Code:
    #include <dos.h>
    
    void outportw(unsigned short port, unsigned short val);
    
    int main()
    {
    	outportw(278, 10);
    	return 0;
    }
    
    void outportw(unsigned short port, unsigned short val)
    {
    	__asm     mov     dx, port
        __asm     mov     ax, val
        __asm     out     dx, ax
    }
    So I debugged the error and this is the code that VS60 turns the ASM into:

    Code:
    void outportw(unsigned short port, unsigned short val)
    {
    00401050  push        ebp  
    00401051  mov         ebp,esp 
    00401053  sub         esp,40h 
    00401056  push        ebx  
    00401057  push        esi  
    00401058  push        edi  
    00401059  lea         edi,[ebp-40h] 
    0040105C  mov         ecx,10h 
    00401061  mov         eax,0CCCCCCCCh 
    00401066  rep stos    dword ptr [edi] 
    	__asm     mov     dx, port
    00401068  mov         dx,word ptr [port] 
        __asm     mov     ax, val
    0040106C  mov         ax,word ptr [val] 
        __asm     out     dx, ax
    00401070  out         dx,ax 
    }
    I have tried this through MASM as well, and the same thing happens. I would appreciate any insight.

  2. #2
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    BTW

    I have also tried using _outp, and _inp. They both crash the application. I am doing everything correctly, I have no idea why 'Debug' in the CommandPrompt should work and nothing else.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    CreateFile allows you to open a handle to a Parallel port....

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    70
    I dont mess around with inline asm when Im working with C/++ but, I do know that alot of asemblers (I dont use MASM so I dont know if its true for MASM...) dont like spaces where they shouldnt be.
    so like
    out dx, ax
    would be
    out dx,ax
    and since your doing dx,ax Im assuming you want a Word to be sent out right?
    "...since anyone who is anyone knows C..." -Peter Cellik

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by KrAzY CrAb
    I dont mess around with inline asm when Im working with C/++ but, I do know that alot of asemblers (I dont use MASM so I dont know if its true for MASM...) dont like spaces where they shouldnt be.
    so like
    out dx, ax
    would be
    out dx,ax
    and since your doing dx,ax Im assuming you want a Word to be sent out right?
    It's more likely to be what Salem has already pointed out....

    Most likely the OP's compiler is a windows compiler.....therefore the code is run in user mode and so the OS doesnt allow the code to access certain features unless they are via published routes (APIs). This applies exactly the same to MASM (assuming you are using one of the directives over .386)

    The reason it works in debug is that debug is a dos program, and so windows runs it in dos emulation mode.....the dos prog thinks it has direct access to these features, but infact the os takes the request to access the port and converts it into a normal system service and that completes and returns to the code....

    So the OP has 2 options - program a dos program with a dos compiler and hope that the code doesnt do something which the emulation will refuse to do......or go down the proper road and use a published API (look out for CreateFile and DeviceIOControl to start)

  6. #6
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    I just found out the problem. Windows NT based systems have a Protection called Privileged Instruction etc... So that if your application does not have ring 0 access to hardware then the CPU wont process the instruction. I have an example of how to bypass it with afew undocumented calls, and how to build a Device Driver to allow access.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Giving instruction to an executing C code
    By darkwalk in forum C Programming
    Replies: 2
    Last Post: 10-24-2006, 01:01 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. When your code crashes
    By caroundw5h in forum C Programming
    Replies: 9
    Last Post: 06-19-2004, 10:40 PM
  4. Merging files (code included)
    By TankCDR in forum C Programming
    Replies: 3
    Last Post: 10-28-2001, 11:06 AM
  5. why does the following code crashes
    By psychedelic_fur in forum Linux Programming
    Replies: 1
    Last Post: 10-09-2001, 12:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21