News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Windows Assembly Language Programming

Started by Joe Btfsplk, February 24, 2011, 06:26:52 PM

Previous topic - Next topic

Joe Btfsplk

I hope this topic is where we can discuss Windows Assembly Language Programming.

Note that I have ordered the Windows Assembly Language Programmers Guide from Amazon but it isn't here yet.

In DOS Assembly Language I use the following code to get more memory:

      MOV     BX,(No. of 16 byte words I want)
      MOV     AX, 48h
      INT       21H
      JC         ERROR
       (The address of the memory I acquired is in the AX register)

The problem with DOS is that I can't get more than 64K of memory in a segment. I hope that the book I ordered will tell me how to get more than 64K of memory using Windows. If anyone knows how to do it, please post the Assembly Language code it takes in Windows. (Note that I don't see how you can get more than 64K of memory in a segment because the DS, ES, FS, and GS registers are only 16 bit registers. How would you address above 64K of a memory segment?)

Joe Btfsplk
Dinosaur Mainframe programmer
experienced DOS Assembly Language Programmer.
Converting to Windows Assembly Language programming.

clive

Unless you are doing 16-bit Windows code the segment size of 64 KB shouldn't be a problem. And you don't want to be doing math on them.

In 32-bit Windows, they are SELECTORS, referring to descriptors in a table, and can describe up to 4 GB of addressing space, although the generally usable range is 2-3 GB. This address space is virtualized through a page table to the physical memory, or page file.

Check the Win32 API functions LocalAlloc(), GlobalAlloc(), HeapAlloc(), VirtualAlloc(), etc
http://msdn.microsoft.com/en-us/library/aa366781%28v=vs.85%29.aspx
It could be a random act of randomness. Those happen a lot as well.

dedndave

 :bg
in 32-bit code, we use GlobalAlloc, LocalAlloc, or HeapAlloc and related functions
you can grab as much memory as the user has available
64 Kb is considered as a fairly "small" amount

unless a situation requires otherwise, i usually use HeapAlloc...
       INVOKE  GetProcessHeap
       mov     hHeap,eax
       INVOKE  HeapAlloc,eax,NULL,BytesRequired
       mov     hBlock,eax

if you want the memory zeroed, replace the NULL with HEAP_ZERO_MEMORY
hBlock represents the base address of the allocated block
when you are done using it.....
       INVOKE  HeapFree,hHeap,NULL,hBlock

http://msdn.microsoft.com/en-us/library/aa366781(v=VS.85).aspx

redskull

Quote from: Joe Btfsplk on February 24, 2011, 06:26:52 PM
Note that I don't see how you can get more than 64K of memory in a segment because the DS, ES, FS, and GS registers are only 16 bit registers. How would you address above 64K of a memory segment?)

In protected mode, the 16-bit values in the segment registers no longer directly point to base addressess; they are integer indexes of a row in an entirely seperate memory-based table (the LDT/GDT), which contain 32-bit base address (among other things).  The CPU does all the calculations, and the operating system maintains the table.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

clive

This book might be a good place to start with to understand the mechanics of what changed on the x86 when it moved from 16-bit to 32-bit

The 80386 Book by Ross P Nelson
http://www.alibris.com/booksearch?qsort=p&isbn=1556151381

another would be

Programming the 80386 by John Crawford, Patrick P Gelsinger
http://www.alibris.com/booksearch?qwork=5407960

You could also try the current Intel manual set, although I think the older ones might be more helpful to grasp the sea change that occurred. The ones on 386 Systems Programming might be particularly interesting from an OS/Assembler perspective.
It could be a random act of randomness. Those happen a lot as well.

BogdanOntanu

Also do not forget the VirtualAlloc (and VritualFree/VirtualQuerry) where you usually allocate much larger blocks (64K is a minimum reserve) and at time you have to manage more but you have the option to reserve and commit memory in chunks when you need it ;)

Basically in Windows one does not use INT 21h anymore. Also the use or 16 bits registers like ax,bx is more rare and instead we use 32bits registers like eax,ecx,edx,ebx,esp,ebp,esi,edi. We even have 64bits and 128 bits registers available.

You need to learn how to use the Windows API and 32 bits registers.

With mov eax,[esi] you can theoretically access any memory range from zero to 4G (if you have the rights of course) and because of this the segment registers are not needed anymore.

There is a whole new (and modern) ASM world there.

I suggest that you check Iczelion's famouse ASM tutorials online ...  for a faster intro while you wait for the books you ordered ;). 

BTW... books about modern ASM programming (32bits and 64bits) are rare and not so good anyway because ASM is no longer perceived as a mainstream or even an used programming language of today.

Books can help you with Java and C/C++ and C# and Python but not with ASM. There are not enough people interested in ASM in order to buy (and hence write) ASM books anymore.

Hence you might be forced to learn yourself and/or to ask questions on this forums ;)



Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

dedndave

here's that book Clive mentioned in PDF form...

http://ebookee.org/The-80386-Book_558870.html

i just saved you 99 cents   :P

clive

Quote from: dedndave
i just saved you 99 cents   :P

And probably $3.99 S/H too. I did try to find a PDF while looking for a cite. Personally prefer real books though.

Anyway consider Alibris, most of the Amazon book sellers are there, and the coupon codes are easier to find.
It could be a random act of randomness. Those happen a lot as well.

Joe Btfsplk

Quote from: dedndave on February 24, 2011, 07:07:43 PM
here's that book Clive mentioned in PDF form...

http://ebookee.org/The-80386-Book_558870.html

i just saved you 99 cents   :P

I have that book and it's a pretty good book on the architecture of the 80386 (and above) but not much good information on converting Assembly Language programs from DOS to Windows. I hope the book I've ordered does a better job of explaining what instructions to use to replace the INT 21H instructions. I did learn from it that I need to convert all the registers that I PUSH onto the stack into 32 bits so the great sum of $4.50 that I spent on it wasn't completely wasted.

Thanks for your concern about my finances dave!

Joe Btfsplk
Learning Windows Assembly Language,
because Assembly Language is the best language for Systems Software.
(Not very good for applications, COBOL is the best for applications, easyest to follow program logic.)

Joe Btfsplk

Quote from: clive on February 24, 2011, 06:38:16 PM
In 32-bit Windows, they are SELECTORS, referring to descriptors in a table, and can describe up to 4 GB of addressing space, although the generally usable range is 2-3 GB. This address space is virtualized through a page table to the physical memory, or page file.

clive:

I also learned that from the 80386 book that I bought, but it doesn't tell me how to ask Windows for the memory I want.

JoeBtfsplk

brethren

if you've installed masm32 then you can allocate a block of memory to use like this
INCLUDE \masm32\include\masm32rt.inc

.data?
pbuf DWORD ?

.code
start:
;allocate 16384 bytes of memory, pbuf contains a pointer
;to the start of the requested memory
mov pbuf, alloc(16384)

;when you've finished with the memory make sure you free it
free(pbuf)

exit
END start

jj2007

Quote from: Joe Btfsplk on February 24, 2011, 10:01:51 PM
it doesn't tell me how to ask Windows for the memory I want.

There are many ways to ask Windows for memory. Here is one - pure Masm:

Quoteinclude \masm32\MasmBasic\MasmBasic.inc
   
Init
   Let esi=FileRead$("\Masm32\include\Windows.inc")
   Inkey Left$(esi, 1000)
   
Exit
end start

But seriously, you need a basic intro to the Win32 SDK. Try to get the Windows 32-bit API Help File

hutch--

Joe,

Allocate a GIG of memory. (if you have that much on your machine.)


IF 0  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                            Build this template with "ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include\masm32rt.inc

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    LOCAL pMem  :DWORD
    LOCAL gSiz  :DWORD

  ; ------------------------
  ; allocate a GIG of memory
  ; ------------------------
    invoke GlobalAlloc,GMEM_FIXED or GMEM_ZEROINIT,1024*1024*1024
    mov pMem, eax

  ; ------------
  ; get its size
  ; ------------
    invoke GlobalSize,pMem
    mov gSiz, eax

  ; --------------------------
  ; display the allocated size
  ; --------------------------
    fn MessageBox,0,ustr$(gSiz),"This much",MB_OK

  ; ----------------------------
  ; release the allocated memory
  ; ----------------------------
    invoke GlobalFree,pMem

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

clive

Quote from: Joe Btfsplk on February 24, 2011, 10:01:51 PM
I also learned that from the 80386 book that I bought, but it doesn't tell me how to ask Windows for the memory I want.

Which is why I provided a link to the Win32 API functions most relevant to your question. There are are some extensive books that list functions, but the on-line resource is usually quicker and more up to date.

QuoteCheck the Win32 API functions LocalAlloc(), GlobalAlloc(), HeapAlloc(), VirtualAlloc(), etc
http://msdn.microsoft.com/en-us/library/aa366781%28v=vs.85%29.aspx
It could be a random act of randomness. Those happen a lot as well.

Joe Btfsplk

Somewhere I heard about a DOS extender?

Is this so that I can use 32 bit instructions in a DOS program? (If it is it would make it a whole lot simpler to convert DOS programs to winows 32 bit because I could test the changes I've made part way through the process of converting a 16 bit DOS program to a 32 bit Windows program. The changes I've made so far compile OK but it crashes when I try to run it.)

If there is a 32 bit DOS extender, how do I get it?

Joe Btfsplk