News:

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

Main Menu

Hello World Question

Started by luciddreamer, January 25, 2021, 07:15:25 PM

Previous topic - Next topic

luciddreamer

Example:

.386
option casemap :none
include \masm32\include\masm32rt.inc

.data
message  db "Hello World",13,10,0
func     db "pause",0


.code
main:
    invoke  StdOut,ADDR message
    invoke  crt_system,ADDR func
    invoke  ExitProcess,0
END main


PSEUDO C OUTPUT:

void __noreturn start()
{
  sub_401020(aHelloWorld);
  system(Command);
  ExitProcess(0);
}

//sub_401020 code probably func to StdOut
DWORD __stdcall sub_401020(LPCVOID lpBuffer)
{
  DWORD nNumberOfBytesToWrite; // [esp+0h] [ebp-Ch]
  DWORD NumberOfBytesWritten; // [esp+4h] [ebp-8h] BYREF
  HANDLE hFile; // [esp+8h] [ebp-4h]

  hFile = GetStdHandle(0xFFFFFFF5);
  nNumberOfBytesToWrite = sub_401060(lpBuffer);
  WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, &NumberOfBytesWritten, 0);
  return NumberOfBytesWritten;
}

Dissasembled Assembly

.text:00401000                 public start
.text:00401000 start           proc near
.text:00401000                 push    offset aHelloWorld ; "Hello World\r\n"
.text:00401005                 call    sub_401020
.text:0040100A                 push    offset Command  ; "pause"
.text:0040100F                 call    ds:system
.text:00401015                 add     esp, 4
.text:00401018                 push    0               ; uExitCode
.text:0040101A                 call    ExitProcess
.text:0040101A start           endp

Vortex

Hi luciddreamer,

I would suggest you Agner Fog's objconv tool, it's a very good disassembler :

https://github.com/gitGNU/objconv

luciddreamer

Quote from: Vortex on January 26, 2021, 02:09:17 AM
Hi luciddreamer,

I would suggest you Agner Fog's objconv tool, it's a very good disassembler :

https://github.com/gitGNU/objconv

Thank you, I will try it out now.

Dan-TheStarman

Quote from: luciddreamer on January 25, 2021, 11:46:01 PM
Played around with Assembly, what does ADDR operator do?

Hi luciddreamer,

   I know I'm a bit late here, but with your background this may have been more helpful: ADDR is essentially what you'd think of as PTR; a POINTER to where some data can be found, or an "lp<name>" parameter in VS Windows code.  I didn't realize that when I first started working with masm32 quite recently.

   Apart from using "inkey" in console code, I wanted to say that it's pretty easy in masm32 (unlike Windows C++ etc. code) to just go ahead and use "MessageBox" or "MessageBoxA" functions that wait for someone to click on "OK" to close them! It's what the average Windows users expect these days anyway instead of a Command Prompt window.  They'll be impressed, and you'll be like: It was so easy!

MB_OK button = "0"; same thing with an INFO icon = "40h".  I did a very extensive "Hello" program in VS2019 using 64-bit Assembly code (the free Community edition requires you to use the C++ code that Viorel-1 supplied; here's URL:
https://docs.microsoft.com/en-us/answers/questions/169851/error-lnk2001-unresolved-external-symbol-winmaincr.html#answer-170564), but here's what the main section from there would become in masm32 (32-bit runs just fine under Windows 10 latest versions):

; Set up the parameters for calling the "MessageBoxA" Function (see:
; https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-messageboxa
; for more information on all the available parameters you could use):

invoke MessageBoxA, 0, addr winTxt, addr winCapt, INFOI
invoke ExitProcess, 0   ; Exit Code = "0".


With an "INFOI  EQU 40h" earlier in program; or use "MB_OK" or "0" for just an "OK" button.  You could also use a different exit code, or alternatively set it up for changing depending upon the code in a previous invoke (function call).  The spaces are optional, "addr" makes it a pointer to the messages, and the most important thing of all: You don't need to keep track of any specific registers or what order they need to be in: You use the SAME ORDER they occur in the Microsoft API documentation; like you would for C++, etc.


Dan, TheStarman

hutch--

There is another way to get the swing of the ADDR operator, you can directly code this in mnemonics (instructions).

LOCAL MyVar :DWORD    ; the variable
LOCAL pVar  :DWORD    ; a pointer to it

    lea eax, MyVar    ; load the address of the memory operand into eax
    mov pVar, eax     ; store the address into a DWORD sized pointer