Author Topic: Call Subroutine  (Read 705 times)

Militeignoto

  • Regular Member
  • *
  • Posts: 2
Call Subroutine
« on: December 14, 2020, 10:20:27 AM »
Hi

My name is Giorgio , I'm a first-year student(high School).
nice to "meet" you

I'm learning assembly and I'm trying to figure out how to make subroutines. I've modified the hello world example.
but I don't understand this push

Code: [Select]
Push 0, Addr RCKEEP      ;RCKEEP receives output from API



Code: [Select]
Data Section
;
RCKEEP DD 0    ;temporary place to keep things

toConsole  DD 0 ; HANDLE WINAPI GetStdHandle(
  ;   _In_ DWORD nStdHandle
; )

STD_OUTPUT_HANDLE Equ -11D ; 11 The standard output device.Initially, this is the active console screen buffer.


Code Section
;
START:

/* Initialize the console */
Push STD_OUTPUT_HANDLE ;STD_OUTPUT_HANDLE
Call GetStdHandle        ;get, in eax, handle to active screen buffer
Mov [toConsole], Eax


Call HELLO1
Call HELLO2
Call EXIT

HELLO1:
Push 0, Addr RCKEEP      ;RCKEEP receives output from API
Push 24D, 'Hello World (from GoAsm)'     ;24=length of string
Push [toConsole]            ;handle to active screen buffer
CALL WriteFile
Ret


HELLO2:
Push 0, Addr RCKEEP      ;RCKEEP receives output from API
Push 24D, 'Hello World again (from GoAsm)'      ;24=length of string
Push [toConsole]            ;handle to active screen buffer
CALL WriteFile
Ret


EXIT:
Xor Eax, Eax              ;return zero
Ret

What it means when it says "receives output" ? Why Does it "receive"?

Thanks friends

EDIT: updated question

Yuri

  • Member
  • **
  • Posts: 179
Re: Call Subroutine
« Reply #1 on: December 14, 2020, 02:33:33 PM »
Hi Giorgio,

The 4th parameter of the WriteFile function is a memory address where the function will put the number of actually written bytes. If you don't need it, you can push 0 instead of the address.

Militeignoto

  • Regular Member
  • *
  • Posts: 2
Re: Call Subroutine
« Reply #2 on: December 15, 2020, 05:23:53 AM »
Thanks I reread the goAsm manual and the winapi Doc.
but I still have a bug to be solved

Code: [Select]
Data Section
;
RCKEEP DD 0    ;temporary place to keep things

toConsole  DD 0 ; HANDLE WINAPI GetStdHandle(
  ;   _In_ DWORD nStdHandle
; )

String1 DB 'Hello friend', 0
String2 DB 'Hello FRIEND', 0


STD_OUTPUT_HANDLE Equ -11D ; 11 The standard output device.Initially, this is the active console screen buffer.


Code Section
;
START:

/* Initialize the console */
Push STD_OUTPUT_HANDLE ;STD_OUTPUT_HANDLE
Call GetStdHandle        ;get, in eax, handle to active screen buffer
Mov [toConsole], Eax


Call HELLO1
Call HELLO2
Call EXIT

HELLO1:
Push 0, Addr RCKEEP      ;RCKEEP receives output from API
Mov Esi, Addr String1 ;gets pointer to
Push 13D, Esi ;13=length of string1
Push [toConsole]            ;handle to active screen buffer
CALL WriteFile
Ret


HELLO2:
Push 0, Addr RCKEEP      ;RCKEEP receives output from API
Mov Esi, Addr String2 ;gets pointer to
Push 13D, Esi ;13=length of string2
Push [toConsole]            ;handle to active screen buffer
CALL WriteFile
Ret


EXIT:
Xor Eax, Eax              ;return zero
Ret

It repeats 3 time "hello friend" .It should prints only two messages
maybe the EXIT code is the issue

Yuri

  • Member
  • **
  • Posts: 179
Re: Call Subroutine
« Reply #3 on: December 15, 2020, 01:53:59 PM »
You need one more 'ret' — at the end of the START routine. Without it, execution goes through HELLO1, up to its 'ret'.

Code: [Select]
Call HELLO1
Call HELLO2
Call EXIT
ret     ; <—— here.