News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

New member here, need some guidance to start

Started by kcvinu, June 06, 2024, 07:03:03 AM

Previous topic - Next topic

NoCforMe

Very interesting.
Just to be clear, this is only for 64-bit apps, correct?
Assembly language programming should be fun. That's why I do it.

Mikl__

Hi NoCforMe!
Yes, I haven't worked with 32-bit applications for about 10 years. Although Jeffrey Richter may have written this for 32- and 64-bit applications as well

TimoVJL

; ml64.exe hello64m.asm
includelib msvcrt
extern printf : proc
extern exit : proc

.data
msg  db "Hello world!",0

.code
mainCRTStartup proc ; for link.exe if main defined
  sub rsp, 28h
  call main
mainCRTStartup endp

main proc
    sub rsp, 28h
    mov rcx, offset msg
    call printf
    call exit
main endp
end
link.exe predefined names
Generated by HexEdit
C:\code\msvc6\bin\link.exe

000082c0  5f 77 6d 61 69 6e 00 00 5f 6d 61 69 6e 00 00 00  _wmain.._main...
000082d0  77 6d 61 69 6e 00 00 00 6d 61 69 6e 00 00 00 00  wmain...main....
000082e0  5f 77 57 69 6e 4d 61 69 6e 00 00 00 5f 57 69 6e  _wWinMain..._Win
000082f0  4d 61 69 6e 00 00 00 00 77 57 69 6e 4d 61 69 6e  Main....wWinMain
00008300  00 00 00 00 57 69 6e 4d 61 69 6e 00 5f 77 57 69  ....WinMain._wWi
00008310  6e 4d 61 69 6e 40 31 36 00 00 00 00 5f 57 69 6e  nMain@16...._Win
00008320  4d 61 69 6e 40 31 36 00 2e 76 78 64 00 00 00 00  Main@16..vxd....

Generated by HexEdit
link.exe

00008440  5f 44 6c 6c 4d 61 69 6e 43 52 54 53 74 61 72 74  _DllMainCRTStart
00008450  75 70 40 31 32 00 00 00 5f 44 6c 6c 4d 61 69 6e  up@12..._DllMain
00008460  43 52 54 53 74 61 72 74 75 70 00 00 5f 5f 50 6f  CRTStartup..__Po
00008470  73 69 78 50 72 6f 63 65 73 73 53 74 61 72 74 75  sixProcessStartu
00008480  70 00 00 00 77 6d 61 69 6e 43 52 54 53 74 61 72  p...wmainCRTStar
00008490  74 75 70 00 6d 61 69 6e 43 52 54 53 74 61 72 74  tup.mainCRTStart
000084a0  75 70 00 00 77 57 69 6e 4d 61 69 6e 43 52 54 53  up..wWinMainCRTS
000084b0  74 61 72 74 75 70 00 00 57 69 6e 4d 61 69 6e 43  tartup..WinMainC
000084c0  52 54 53 74 61 72 74 75 70 00 00 00 4e 74 50 72  RTStartup...NtPr
000084d0  6f 63 65 73 73 53 74 61 72 74 75 70 00 00 00 00  ocessStartup....
May the source be with you

NoCforMe

Why not simplify things and just do this?

Quote from: TimoVJL on June 08, 2024, 09:36:21 AM; ml64.exe hello64m.asm
includelib msvcrt
extern printf : proc
extern exit : proc

.data
msg  db "Hello world!",0

.code
main proc ; for link.exe so "main()" is defined
  sub rsp, 28h
  mov rcx, offset msg
  call printf
  call exit
main endp

end

In other words, what reason is there to have a proc-within-a-proc? Just put everything in main().

(Not a 64-bit programmer here)
Assembly language programming should be fun. That's why I do it.

TimoVJL

Because of this:
LINK : error LNK2001: unresolved external symbol mainCRTStartupmainCRTStartup is default entry
main is default for console mode application
May the source be with you

NoCforMe

So the linker forces you to have both mainCRTStartup() and main()?

How weird.
Assembly language programming should be fun. That's why I do it.

jj2007

Quote from: Mikl__ on June 08, 2024, 08:28:26 AMin a project you don't have to specify the /SUBSYSTEM linker switch at all... the linker itself will determine the subsystem of your application if it checks which of the four functions (WinMain, wWinMain, main or wmain) is present in your code, and Based on this, it will select a subsystem

That's good to know, Mikl. However, in my projects I need Subsystem:console for debugging (error messages, deb), and Windows for the release version. Therefore I believe in telling the linker explicitly where to enter.

Btw fall thru is another legal option, too:
main proc
main endp
start proc
start endp
mainCRTStartup proc
mainCRTStartup endp
WinMain proc
WinMain endp
entry_point proc
entry_point endp
... meaning "dear linker, pick whatever you like most but please don't generate any extra code"

Mikl__

Ciao Jochen! Come stai? Sono felice che ti sia piaciuto il mio post. La conoscenza non è mai superflua...

jj2007

Ciao Mikl! Я в порядке, спасибо! Всегда приятно, когда наш величайший 64-битный эксперт оказывает нам честь.

tenkey

Quote from: NoCforMe on June 08, 2024, 09:57:56 AMSo the linker forces you to have both mainCRTStartup() and main()?
The "problem" with main() is that by default the linker links in the C++ startup code (mainCRTStartup). One way to prevent that: specify /ENTRY:main to the linker.

zedd151

#55
@tenkey:  :thumbsup:
Exactly.

This is 64 bit assembly under discussion after all, not C.  :biggrin:
We (assembly language programmers) are not limited to "C language" pre-defined entry points, so must tell the linker where our entry point is located.
:azn:

NoCforMe

Quote from: Mikl__ on June 08, 2024, 08:28:26 AMFound it in Jeffrey Richter's book "Windows. Creating effective Win32 applications taking into account the specifics of 64-bit versions of Windows" the following lines
Quote...not many people know that in a project you don't have to specify the /SUBSYSTEM linker switch at all... the linker itself will determine the subsystem of your application if it checks which of the four functions (WinMain, wWinMain, main or wmain) is present in your code, and Based on this, it will select a subsystem...
|                    Application type                          |entry function|  linker key      |
+-------------------+-------------------------------------------+--------------+------------------+
|GUI application    |working with ANSI characters and strings  |WinMain      |/SUBSYSTEM:WINDOWS|
|GUI application    |working with Unicode characters and strings|wWinMain      |/SUBSYSTEM:WINDOWS|
|console application|working with ANSI characters and strings  |main          |/SUBSYSTEM:CONSOLE|
|console application|working with Unicode characters and strings|wmain        |/SUBSYSTEM:CONSOLE|

@Mikl_, this is important and useful enough information that it should be made "sticky" somewhere for easy reference.
Assembly language programming should be fun. That's why I do it.