Author Topic: Resource compiler for console applications  (Read 17725 times)

nidud

  • Member
  • *****
  • Posts: 1371
    • https://github.com/nidud/asmc
Resource compiler for console applications
« on: October 24, 2012, 12:59:39 AM »
Hi all

One of the first programs I wrote was a 16-bit resource editor, and most of the programs I have made after that has been based on using this tool. It is very simplistic and hard to handle in some cases, but it still works.

The programs I made are still used (mostly by old farts), but the main problem with them is the lack of DOS support in newer hardware, especially printers. I also have a small baby project online, which I have been nursing for some time, but now MS is blocking the use of such applications even in XP.

Most of these programs are written in C/C++, so the plan was to convert the resource compiler to 32-bit instead of rewriting all this code. My knowledge of assembly is from 16-bit TASM, so I’m a bit rusty with regards to 32-bit MASM.

The learning curve will be to grind through the console functions first, using kernel32.lib and a few functions from user32.lib (clipboard functions).

The first problem I run into was the color setup, and here is some of the code used for manipulating colors used in 16-bit code:

Code: [Select]
include clib.inc
include conio.inc
include mouse.inc

.code

setpal proc _CType public uses bx? palid:size_t, clid:size_t
test console,CON_COLOR
jz setpal_end
ifndef __f__
mov ax,palid
mov dx,clid
mov     ch,al
mov     bx,dx
mov     ax,0040h
mov     es,ax
mov     dx,es:[0063h]
cmp dx,03D4h
jne setpal_end
cli
mov dx,03DAh
in al,dx
mov dx,03C0h
mov     al,bl
out     dx,al
mov     al,ch
out     dx,al
mov dx,03DAh
in      al,dx
mov dx,03C0h
mov     al,20h
out     dx,al
sti
else
; missing 32-bit code
endif
    setpal_end:
ret
setpal endp

resetpal proc _CType public
push si?
xor si?,si?
.repeat
    invoke setpal,si?,si?
    inc si?
.until si? == 8
mov si?,56
.repeat
    mov ax?,si?
    add ax?,-48
    invoke setpal,si?,ax?
    inc si?
.until si? == 64
invoke setpal,0014h,0006h
pop si?
ret
resetpal endp

getxya proc _CType public x:size_t, y:size_t
ifndef __f__
invoke getxyw,x,y
mov al,ah
mov ah,0
else
    local lpCharacter:dword, lpNumberOfAttributesRead:dword
mov eax,y
shl eax,16
add eax,x
mov ecx,eax
invoke ReadConsoleOutputAttribute,
    OFSH_OUTPUT,addr lpCharacter,1,ecx,addr lpNumberOfAttributesRead
mov eax,lpCharacter
and eax,00FFh
endif
ret
getxya endp

scputa proc _CType public uses ax? dx? x:size_t, y:size_t, l:size_t, a:size_t
 ifndef __f__
mov al,byte ptr x
mov ah,byte ptr y
call __getxypm
invoke wcputa,dx::ax,l,a
ShowMouseCursor
 else
local pcx:dword
push ecx
movzx ecx,byte ptr a
movzx eax,byte ptr x
movzx edx,byte ptr y
shl edx,16
mov dx,ax
invoke FillConsoleOutputAttribute,OFSH_OUTPUT,ecx,l,edx,addr pcx
pop ecx
 endif
ret
scputa endp

end

The code assign value [0..255] to index [0..15]

So, to my first question: is there a simple way to rearrange the 256 palettes like this in 32-bit, or do I have to apply a table of some sort for that?

The library used for this code could be downloaded from this site: https://sourceforge.net/projects/doszip/files/

ToutEnMasm

  • Member
  • *****
  • Posts: 1189
    • EditMasm
Re: Resource compiler for console applications
« Reply #1 on: October 24, 2012, 01:14:55 AM »
see this sample who create a palette of 256 colors.
http://masm32.com/board/index.php?topic=813.msg7226#msg7226
and forget all you know on the 16 bits.
Fa is a musical note to play with CL

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: Resource compiler for console applications
« Reply #2 on: October 24, 2012, 01:48:48 AM »
you have a lot of ground to cover before you even think about re-writing the app for 32-bit

software INT's - a thing of the past
segmented memory model - same
manipulating the vga registers directly - not likely

start out by learning some 32-bit basics, then move into graphics
if you install the masm32 package, have a look at the examples and help folders

nidud

  • Member
  • *****
  • Posts: 1371
    • https://github.com/nidud/asmc
Re: Resource compiler for console applications
« Reply #3 on: October 24, 2012, 03:39:48 AM »
you have a lot of ground to cover before you even think about re-writing the app for 32-bit

software INT's - a thing of the past
segmented memory model - same
manipulating the vga registers directly - not likely

start out by learning some 32-bit basics, then move into graphics
if you install the masm32 package, have a look at the examples and help folders
It is difficult to understand the 16-bit segmented memory models, but it is the foundation of it all. If you understand it, the 32/64-bit is very simple. The library is already converted to 32-bit, and it took less than a week to do so. There is however some adjustment and testing to make it work properly. Since all the software interrupts in the 16-bit code is handled by the OS, in this case 32-bit, the same functionality should be accessible in 32-bit as well.

The software interrupt below, and the 32-bit call will end up calling the same function. The only difference is the interface of the call. The first one is easy the latter is difficult.
Quote
mkdir   proc _CType public directory:dword
  ifdef __f__
   .if func(CreateDirectory,directory,0)
       sub eax,eax
   .else
       call osmaperr
   .endif
   ret
  else
    ifdef __LFN__
   cmp   _ifsmgr,0
    endif
   push   ds
   lds   dx,directory
   mov   ah,39h
    ifdef __LFN__
   je   @F
   stc
   mov   ax,7139h
      @@:
    endif
   int   21h
   pop   ds
   jc   mkdir_err
   xor   ax,ax
    mkdir_end:
   ret
    mkdir_err:
   call   osmaperr
   jmp   mkdir_end
  endif
mkdir   endp

The console mode applications do not use graphics, it is called text mode, and that’s way I like it (no irritating moving objects).

The missing thing here is illustrated in this picture:
(dont't now how to insert a bitmap, so, Start->Run->cmd.exe, right-clikc top of window->property->color)

So the question is (still), is there anybody who know anything about the function used in this API?

jj2007

  • Member
  • *****
  • Posts: 7558
  • Assembler is fun ;-)
    • MasmBasic
Re: Resource compiler for console applications
« Reply #4 on: October 24, 2012, 03:52:56 AM »
So the question is (still), is there anybody who know anything about the function used in this API?

Which function used in which API?

Also, when you post sources, do not assume that we have these includes, or that we know what mouseon etc do.

Code: [Select]
include clib.inc
include conio.inc
include mouse.inc

.code

main proc c
invoke mouseon
or byte ptr console,CON_COLOR
.if !func(editpal)
    invoke resetpal
.else
   and console,not CON_REVGA
.endif
invoke mouseoff
sub ax?,ax?
ret
main endp

    end

The typical template used here looks like this:
include \masm32\include\masm32rt.inc

.code
start:   nop
   exit

end start


Anything more idiosyncratic is likely to get ignored by forum members (because it means wasting time to rewrite the whole thing).

Oh, and before I forget: Welcome to the Forum :icon14:

japheth

  • Guest
Re: Resource compiler for console applications
« Reply #5 on: October 24, 2012, 03:59:18 AM »
Quote
So the question is (still), is there anybody who know anything about the function used in this API?

Well, yes.  :bgrin:

There is no equivalent in the Win32 API to set the VGA attribute controller registers or DACs in text mode ( console ). You are more or less stuck to the 16 default colors.


nidud

  • Member
  • *****
  • Posts: 1371
    • https://github.com/nidud/asmc
Re: Resource compiler for console applications
« Reply #6 on: October 24, 2012, 04:41:25 AM »
Quote
So the question is (still), is there anybody who know anything about the function used in this API?

Well, yes.  :bgrin:

There is no equivalent in the Win32 API to set the VGA attribute controller registers or DACs in text mode ( console ). You are more or less stuck to the 16 default colors.
Thanks!

So the 16-bit console still rules  :biggrin:

Quote
Anything more idiosyncratic is likely to get ignored by forum members (because it means wasting time to rewrite the whole thing).
Don’t waste your time on this jj, it’s complicated: stick to the Basic, its easy and comfy   :P
Quote
Oh, and before I forget: Welcome to the Forum
Thanks jj!

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: Resource compiler for console applications
« Reply #7 on: October 24, 2012, 04:43:21 AM »
i am not sure that is strictly true
some functions, like RealizePalette, may alter the vga palette registers, indirectly
i haven't played with it much - it seems it would make more of a difference if i were using a 256-color mode
because newer systems have far improved graphics capabilities, we don't have as much need to do it

keep in mind, that if you were using a 256-color mode, and you changed the palette,
it would affect all visible windows, including the desktop

ToutEnMasm

  • Member
  • *****
  • Posts: 1189
    • EditMasm
Re: Resource compiler for console applications
« Reply #8 on: October 24, 2012, 04:45:29 AM »
Quote
So the question is (still), is there anybody who know anything about the function used in this API?
The ddk is a good source for this.It's the perfect level where they can be used.
There is a graphic vga card sample.
firmware can also give some code.
The API are here to do not know all the material details and normalize there use.

Fa is a musical note to play with CL

nidud

  • Member
  • *****
  • Posts: 1371
    • https://github.com/nidud/asmc
Re: Resource compiler for console applications
« Reply #9 on: October 24, 2012, 05:32:45 AM »
Quote
Since all the software interrupts in the 16-bit code is handled by the OS, in this case 32-bit, the same functionality should be accessible in 32-bit as well.
The property dialog box lets you set the value for each of the 16 colours from 0..255, so I think it must be possible.
Quote
So the question is (still), is there anybody who know anything about the function used in this API?
The ddk is a good source for this.It's the perfect level where they can be used.
There is a graphic vga card sample.
firmware can also give some code.
The API are here to do not know all the material details and normalize there use.
This is probably the solution, but
Quote
you have a lot of ground to cover before you even think about re-writing the app for 32-bit
...
start out by learning some 32-bit basics, then move into graphics
this takes time, so
Quote
The learning curve will be to grind through the console functions first, using kernel32.lib and a few functions from user32.lib (clipboard functions).
I will stic to the
Quote
You are more or less stuck to the 16 default colors.
for now, so thanks for the input so far!

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: Resource compiler for console applications
« Reply #10 on: October 24, 2012, 05:46:20 AM »
Code: [Select]
;==============================================================================
include \masm32\include\masm32rt.inc
;==============================================================================
.data
    hwndCon dd 0
    hdcCon  dd 0
.code
;==============================================================================
start:
;==============================================================================

    invoke GetConsoleWindow
    mov hwndCon, eax
    printf("hwndCon %X\n",eax)

    invoke GetDC, hwndCon
    mov hdcCon, eax
    printf("hdcCon %X\n",eax)

    invoke GetDeviceCaps, hdcCon, SIZEPALETTE
    printf("SIZEPALETTE %d\n\n",eax)

    invoke GetDeviceCaps, hdcCon, RASTERCAPS
    mov ebx, eax

    ; RC_BANDING Requires banding support.
    .IF ebx & RC_BANDING
        printf("RC_BANDING\n")
    .ENDIF
    ; RC_BITBLT Capable of transferring bitmaps.
    .IF ebx & RC_BITBLT
        printf("RC_BITBLT\n")
    .ENDIF
    ; RC_BITMAP64 Capable of supporting bitmaps larger than 64 KB.
    .IF ebx & RC_BITMAP64
        printf("RC_BITMAP64\n")
    .ENDIF
    ; RC_DI_BITMAP Capable of supporting the SetDIBits and GetDIBits functions.
    .IF ebx & RC_DI_BITMAP
        printf("RC_DI_BITMAP\n")
    .ENDIF
    ; RC_DIBTODEV Capable of supporting the SetDIBitsToDevice function.
    .IF ebx & RC_DIBTODEV
        printf("RC_DIBTODEV\n")
    .ENDIF
    ; RC_FLOODFILL Capable of performing flood fills.
    .IF ebx & RC_FLOODFILL
        printf("RC_FLOODFILL\n")
    .ENDIF
    ; RC_PALETTE Specifies a palette-based device.
    .IF ebx & RC_PALETTE
        printf("RC_PALETTE\n")
    .ENDIF
    ; RC_SCALING Capable of scaling.
    .IF ebx & RC_SCALING
        printf("RC_SCALING\n")
    .ENDIF
    ; RC_STRETCHBLT Capable of performing the StretchBlt function.
    .IF ebx & RC_STRETCHBLT
        printf("RC_STRETCHBLT\n")
    .ENDIF
    ; RC_STRETCHDIB Capable of performing the StretchDIBits function
    .IF ebx & RC_STRETCHDIB
        printf("RC_STRETCHDIB\n\n")
    .ENDIF

    inkey
    exit
;==============================================================================
end start
I’m not sure what to make of the GetDeviceCaps-RASTERCAPS return value. The console is not a palette-based device, but apparently supports bitmaps.
Code: [Select]
hwndCon 102D4
hdcCon E2010545
SIZEPALETTE 0

RC_BITBLT
RC_BITMAP64
RC_DI_BITMAP
RC_DIBTODEV
RC_FLOODFILL
RC_STRETCHBLT
RC_STRETCHDIB
Well Microsoft, here’s another nice mess you’ve gotten us into.

nidud

  • Member
  • *****
  • Posts: 1371
    • https://github.com/nidud/asmc
Re: Resource compiler for console applications
« Reply #11 on: October 24, 2012, 06:15:39 AM »
i am not sure that is strictly true
some functions, like RealizePalette, may alter the vga palette registers, indirectly
i haven't played with it much - it seems it would make more of a difference if i were using a 256-color mode
because newer systems have far improved graphics capabilities, we don't have as much need to do it

keep in mind, that if you were using a 256-color mode, and you changed the palette,
it would affect all visible windows, including the desktop
The RealizePalette entry in MSDN gives one (of many) example on using CreatePalette, but they are defined in the GDI32.LIB.

To not break the 'kernel32-only-rule', I could create a 16-bit setpal.exe and just .. execute it :lol:

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: Resource compiler for console applications
« Reply #12 on: October 24, 2012, 08:39:00 AM »
i am not aware of a "kernel32-only" rule   :shock:

that is a very crippling rule, as many win32 functions lie outside kernel32

nidud

  • Member
  • *****
  • Posts: 1371
    • https://github.com/nidud/asmc
Re: Resource compiler for console applications
« Reply #13 on: October 24, 2012, 09:25:57 AM »
you have a lot of ground to cover before you even think about re-writing the app for 32-bit
start out by learning some 32-bit basics, then move into graphics
basic console stuff first, then
GDI32.LIB - Graphics Device Interface (GDI)

jj2007

  • Member
  • *****
  • Posts: 7558
  • Assembler is fun ;-)
    • MasmBasic
Re: Resource compiler for console applications
« Reply #14 on: October 24, 2012, 09:32:52 AM »
Don’t waste your time on this jj, it’s complicated: stick to the Basic, its easy and comfy   :P

Well, under the hood it's often not comfy at all :greensml:

By the way, I liked your discussion of the expansion problem, and would support the warning option, especially since the workaround is very straightforward:

include \masm32\MasmBasic\MasmBasic.inc   ; download
  Init
  Let esi="This is a stupid test"
  .if Instr_(esi, "stuupid")
     PrintLine "stuupid found"
  .elseif Instr_(esi, "test")
     PrintLine "test found"
  .endif
  Inkey "Found something??", CrLf$
  .if Instr_(esi, "stuupid")
     PrintLine "stuupid found"
  .else
     .if Instr_(esi, "test")
          PrintLine "test found"
     .endif
  .endif
  Inkey "better??"
  Exit
end start