Yet Some More ASM

Posts 1–14 of 14 · Page 1 of 1
Yet Some More ASM
why isn't this simple working like I want it to. I don't understand what I did wrong, all i did was use WriteConsole, and I want it to Print Heloo Man. Here is my code:


Code:

.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib


.data
writedata db "HELOO MAN", 0
.data?
consoledata db ?

.code

start:
mov eax, 9

push 0
push 0
push eax
push offset writedata
push GENERIC_WRITE
call WriteConsole

push 10000
call Sleep

push 0
call ExitProcess

end start
The second parameter of WriteConsole should be a HANDLE that has GENERIC_WRITE access (invoke GetStdHandle, STD_OUTPUT_HANDLE). The fourth parameter of WriteConsole is not optional.

Use the invoke macro, it is much more readable.
Also, MASM assembles for win32 subsystem by default, you should call AllocConsole() and then use that handle to write data to your console.
Or change MASM to assemble for console. I don't know where you can do that though, since I only ever needed a window.
Code:
 .386 ; 386 Processor Instruction Set

.model flat,stdcall ; Flat memory model and stdcall method

option casemap:none ; Case Sensitive

;Libaries and Include files used in this project

; Windows.inc defines alias (such as NULL and STD_OUTPUT_HANDLE in this code
include \masm32\include\windows.inc 

; Functions that we use (GetStdHandle, WriteConsole, and ExitProcess)
; Listing of all available functions in kernel32.lib
include \masm32\include\kernel32.inc 
; Actuall byte code available of the functions
includelib \masm32\lib\kernel32.lib  

.data
; Labels that with the allocated data (in this case Hello World!...) that are aliases to memory.
output db "Hello World!", 0ah, 0h; This String Hello World! and then a the newline character \n (0ah) and then the null character 0h

.code 
start: 

; --------------------------------------------------------------------------------------------------------------------------------------
; Retrieves that handle to the output console
;
; ====Arguments===
;
; STD_OUTPUT_HANDLE - alias for -11 and indicates that we want the handle to 
;                     write to console output
;
invoke GetStdHandle, STD_OUTPUT_HANDLE
; --------------------------------------------------------------------------------------------------------------------------------------

; --------------------------------------------------------------------------------------------------------------------------------------
; Writes the text in output (.data section) to the console
;
; ====Arguments===
;
; eax - the handle to the console buffer
;
; addr output - pass by reference the text of output (Hello World!)
;
; sizeof output - the size of the string so that the WriteConsole knows when to 
;                 stop (doesn't support NULL terminated strings I guess);
;
; ebx - secondary "return" value that contains the number of bytes written (eax
;       is used for an error code)
;
; NULL - this is reserved and MSDN says just to pass NULL
;
; MSDN Link: http://msdn.microsoft.com/en-us/library/ms687401(v=VS.85).aspx
;
invoke WriteConsole, eax, addr output, sizeof output, ebx, NULL
; --------------------------------------------------------------------------------------------------------------------------------------

; --------------------------------------------------------------------------------------------------------------------------------------
; Exits the program with return code 0 (default one that usually is used to 
; indicate that the program did not error
;
; ====Arguments===
;
; 0 - the exit code
;
; MSDN Link: http://msdn.microsoft.com/en-us/library/ms682658(VS.85).aspx
;
invoke ExitProcess, 0
; --------------------------------------------------------------------------------------------------------------------------------------

end start
This isn't my code btw, it is from another website, but it helped, but i just have a question. Why does eax have the HANDLE? He called the GetStdHandle() function, but there was nothing done to eax. How would i know that the handle is in eax? And how would i know that ebx contains the number of chars written? This is what is confusing me.
  1. The return value is passed through EAX.
  2. MSDN
EAX is the register that usually holds whatever value any function is returning, just like how ECX usually holds the pointer to a class.

If you're writing your own function, making it a habit would probably be ideal.
oh, that makes sense. So, eax always holds the return value of a function and ecx holds the class pointer. What does ebx usually hold?
Quote Originally Posted by 258456 View Post
oh, that makes sense. So, eax always holds the return value of a function and ecx holds the class pointer. What does ebx usually hold?
Usually a base or offset. In NASM, EBX is the only register that can be used for this ( not sure why, maybe it's only in 16-bit ).

Code:
string db "Lolwut",0

main:

mov ebx,3
mov [string + ebx],10 ;some random value, can't think of an example
These are general purpose registers, you can hold whatever you want inside them. ECX won't always have the this pointer. EAX won't always have the return value.
Quote Originally Posted by Fovea View Post
These are general purpose registers, you can hold whatever you want inside them. ECX won't always have the this pointer. EAX won't always have the return value.
Well, then if eax doesn't always have the return value, how would i know if it does or not. This is really confusing me?
After an API call, EAX should contain the return value (unless the API has a return type of void, which means you should ignore whatever EAX has after the API call).
oh well ok, that makes sense, the way you said it made me think that eax just randomly contains return values. Thanks as usual, you are my assembly hero, lol.
Quote Originally Posted by 258456 View Post
oh well ok, that makes sense, the way you said it made me think that eax just randomly contains return values. Thanks as usual, you are my assembly hero, lol.
You can be certain that winapi functions return their stuff in eax, and if it's not it's explicitly stated.
Quote Originally Posted by .::SCHiM::. View Post
You can be certain that winapi functions return their stuff in eax, and if it's not it's explicitly stated.
stated where? MSDN? I use MSDN as a reference, but it is about winapi in c++ it's not about winapi in asm. I just look at the parameters.
Posts 1–14 of 14 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Talk with us