Thread: MASM refuses to output a character

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    MASM refuses to output a character

    After I compile this (MASM32,CONSOLE), I get the Windows screen "bla.exe encountered a problem and needs to close. We are...". Does anyone see anything wrong with this. It should output "a" character to console.
    Code:
    .386
    .model flat,stdcall
    .CODE
    start:
    	mov dl, 97
    	mov ah, 2h
    	int 21h
    	ret
    end start
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That code worked in DOS. It does nothing in Win32 executables, except trigger the OS's "bad program" alarms.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Well, I'd like to do it in console...
    Can I do it with interrupts?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No. You can do it by calling into the C runtime or the Win32 API. WriteConsole exists, I think, or if not, use GetStdHandle to obtain the stdout handle and use any file write API to write to it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You can of course call the win32 API from asm just as easily as you can from C or C++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I know that, I just wondered if it is possible to do it without API calls.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Call the C runtime. Of course, it will only internally call the API in turn.

    Nothing in Windows is possible without API calls if it has to interact with the system in any way. That's the point: the API is the way you interact with the system.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Aren't there ANY working interrupts in Windows?

    I've heard Native API can be accessed through interrupts and I found an example of NtQuerySystemInformation:
    Code:
    NtQuerySystemInformation:
        mov   eax, 97h
        lea   edx, [esp+4]
        int   2Eh
        ret   10h
    Last edited by maxorator; 10-18-2006 at 08:56 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Now I came up with this, it should convert a string to uppercase:
    Code:
    EditAddr proc theaddr:DWORD
        mov edx,theaddr
        mydock:
            mov ax,0
            cmp [edx],ax
            je theend
            mov ax,97
            cmp [edx],ax
            jl runagain
            mov ax,122
            cmp [edx],ax
            jg runagain
            mov ax,32
            sub [edx],ax
        runagain:
            inc edx
            jmp mydock
        theend:
            ret
    EditAddr endp
    It only does it to the last character of the string.
    Last edited by maxorator; 10-18-2006 at 11:22 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Why would you possibly want to do this? I mean, yes, it's probably possible: the fastest way to switch between kernel and user mode on the IA-32 architecture is a software interrupt, so internally, the API probably uses them. But why you, as the end user?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Yeah, I know it's silly, but I just wondered if there are any interrupts still used under Windows.

    Now I know there are some, and that's enough for me
    The problem with my function was that ax register is 16bit and it takes 2 characters together to ax and if the 2nd character is not NULL then it is always over 122. I needed to use ah register instead.
    Last edited by maxorator; 10-19-2006 at 07:09 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The whole 'no interrupts' has to do with how Intel designed the x86 architecture under protected mode. To understand why there are 'no' interrupts I suggest you read the tech refs provided on their site.

    If you insist on doing this from pure asm, then Salem's suggestion will work. Just make sure you know the exact prototype and call method for the function or you will not get what you want. Most of the API is declared as PASCAL which pushes the parameters onto the stack totally reverse of what you are used to.

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Yeah I know, I've been using Win32 API in assembly.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  14. #14
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I know that, I just wondered if it is possible to do it without API calls.
    Yeah I know, I've been using Win32 API in assembly.
    Well seems to me since you know so much you don't need our help eh?

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I know that, I just wondered if it is possible to do it without API calls.
    "that" is what I already knew, the second part means what I didn't know before
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sizeof" character strings - output issue
    By bunko in forum C Programming
    Replies: 3
    Last Post: 12-03-2008, 06:10 PM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Replies: 3
    Last Post: 11-03-2002, 02:14 AM
  5. How do I output one character??
    By kia_1998 in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2001, 10:46 PM