News:

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

Main Menu

My first Msgbox with ASM - Two questions

Started by AssemblyBeginner, February 10, 2015, 09:58:05 AM

Previous topic - Next topic

AssemblyBeginner

Hi all and happy to join the forum

I have a Classic VB background and am just beginning to learn ASM from scratch .. I wanted to start with the famous hello world program
the following works as expected :
;.386
;.model flat,stdcall
; option casemap:none

include \masm32\include\masm32rt.inc
.data
MyTitle db "ASM is Fun!",0
MyText db "I hope you're learning!",0
msg dd 0,0
.code
start:
push 0
mov eax,  offset MyTitle
push eax
mov eax, offset MyText
push  eax
push 0
call MessageBoxA
;push eax
call ExitProcess
end start


Now the two questions :

1- How do I pass a variable containg a numeric value to the MsgBox in the second argument  ie: MyText ( Say I want to display the desktop hwnd via the GetDesktopWindow API
2 - The above code displays the Msgbox but doesn't show how to get the return value from the user and handle it

Can anybody please show me how to do the above ?  I tend to learn by small examples better than reading long tutorials

Regards.

lorenzo

Formatting can be done with wsprintf.
Return value for 32-bit MessageBox is in eax.

I haven't tested the following..mightn't work.

.686
.model flat, stdcall

include windows.inc

includelib user32.lib
includelib kernel32.lib

.data
title db "Handle test", 0
fmt db "Window Handle: %08X", 0
buf db 64 dup (?)
.code

start:
invoke GetDesktopWindow
invoke wsprintf, addr buf, addr fmt, eax
invoke MessageBox, NULL, addr buf, addr title, MB_OK
invoke ExitProcess, 0

end start


If you know some C, you can generate assembler code with MSVC using /Fa switch.
That'll give you idea how it works under the hood.

jj2007

Standard example using Masm32 macros:
include \masm32\include\masm32rt.inc

.code
start: MsgBox 0, str$(rv(GetDesktopWindow)), "Your handle:", MB_YESNO
.if eax==IDYES
MsgBox 0, "You clicked YES", "Hi", MB_OK
.else
MsgBox 0, "You clicked NO", "Hi", MB_OK
.endif
exit

end start


Use Olly to see what's happening under the hood.

dedndave

there are other masm32 macros that may be used, as well

    INVOKE  MessageBox,0,hex$(eax),offset szTitle,MB_OK     ;hexadecimal dword value from register
    INVOKE  MessageBox,0,str$(dwValue),offset szTitle.MB_OK ;signed decimal dword value from memory

ustr$ is for unsigned decimal values

when done, MessageBox returns a result in EAX

https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505%28v=vs.85%29.aspx

    INVOKE  MessageBox,0,offset szText,offset szTitle,MB_YESNO
    .if eax==IDYES
        ;Yes button was pressed
    .elseif eax==IDNO
        ;No button was pressed
    .endif


hutch--

Here is a quick tweak of your code.


include \masm32\include\masm32rt.inc

; ----------------
; initialised data
; ----------------
.data
    MyTitle db "ASM is Fun!",0
    MyText db "I hope you're learning!",0

; ------------------
; uninitialised data
; ------------------
.data?
    num dd ?

; ----------------
; the CODE section
; ----------------
.code
start:

    mov num, ustr$(1234)    ; convert number to string

    push MB_OK              ; push the style
    push offset MyTitle     ; push the title offset
    push num                ; push the string containing the number
    push 0                  ; push 0 as there is no parent handle
    call MessageBox         ; call the ANSI version of messageBox

    push 0                  ; set an exit process value of 0
    call ExitProcess        ; exit the application

end start

AssemblyBeginner

Thanks everybody for responding  8)

This is what i came up with to display the DeskTop hwnd on the MsgBox :
include \masm32\include\masm32rt.inc
; ----------------
; initialised data
; ----------------
.data
    MyTitle db "Desktop hwnd",0
; ------------------
; uninitialised data
; ------------------
.data?
    num dd ?
    stl dd ?
; ----------------
; the CODE section
; ----------------
.code
start:

    mov num, ustr$(GetDesktopWindow)   ; retriev the deskto hwnd and convert hwnd to string
    mov stl , MB_OK
   
    push stl                ; push the style
    push offset MyTitle     ; push the title offset
    push num                ; push the string containing the Desktop hwnd
    push 0                  ; push 0 as there is no parent handle
    call MessageBox         ; call the ANSI version of messageBox
    push 0                  ; set an exit process value of 0
   
    call ExitProcess        ; exit the application

end start


I have seen some classic VB code that runs a Msgbox extracted form ASM via the CallWindowProc API ... I think it is called inline asm
Does anyone know how to convert the above ASM code into VB using this inline asm technic

GoneFishing

Quote from: AssemblyBeginner on February 11, 2015, 02:55:17 AM
...
This is what i came up with to display the DeskTop hwnd on the MsgBox :
...
Also you might like this notation (tested on Windows XP):
include \masm32\include\masm32rt.inc

.code
     start:

     fn MessageBox,0,str$(GetDesktopWindow),"Desktop hwnd",MB_OK

     exit
     
end  start


Quote from: AssemblyBeginner on February 11, 2015, 02:55:17 AM
...
I think it is called inline asm
...
No, it's called a hack . From my VB days I remember I read a tutorial on this "technic"
and wrote simple proggie . Now I forgot all the detail how exactly it was done.
     


AssemblyBeginner

Thanks vertograd,
I think it is shellcode that you store in a vb string or array and then execute the code via the CallWindowProc API .. It is a hack but it opens up many opportunities in VB

jj2007

If you have MasmBasic installed, have a look at the folder \masm32\masmbasic\mb2vb

Shellcode is for script kiddies, it's a hack, as Vertograd rightly called it. With the files in the mb2vb folder, you have access to the complete Masm32 and MasmBasic libraries.

AssemblyBeginner

Thanks jj2007,

Unfortunately the masmbasic folder is not in my masm32 installation
Do you know how to extract shellcode ?  I see that shellcoding is very colse to asm that's why I thought I would ask in this forum
I hope I am not breaking the rules of this forum .. If so, I do apologise & I wont ask again
Regards

jj2007

Quote from: AssemblyBeginner on February 11, 2015, 06:16:10 AM
Thanks jj2007,

Unfortunately the masmbasic folder is not in my masm32 installation

You need to install the library, see http://masm32.com/board/index.php?topic=94.0 and the remarks on JWasm

QuoteDo you know how to extract shellcode ?  I see that shellcoding is very colse to asm that's why I thought I would ask in this forum
I hope I am not breaking the rules of this forum .. If so, I do apologise & I wont ask again
Regards

The rules are strict because shellcode is almost exclusively used by malware writers. Besides, the DLL approach used in \Masm32\MasmBasic\MB2VB\MasmBasicInsertStruct.bas offers a lot more flexibility.