Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Assembly x86 > Re: Calling lib...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 10 of 22 Topic 4620 of 4729
Post > Topic >>

Re: Calling libraries from assembler.

by Frank Kotler <spamtrap@[EMAIL PROTECTED] > Apr 21, 2008 at 05:25 AM

Terence wrote:
> An interesting point in Frank Kotler's posting, is who gets to clean
> up the stack.
> Here Frank is doing it in the main program (obviously because the
> called rountines don't do this for him).
> But I was "brought up" on the Microsoft standard of the CALLED
> subprogram doing a RET N operation to clear the stack. All my hand-
> wriiten subroutine include this, beuase my main program is a Microsoft
> Fortran program which folows my description of the stack
> responsibility.
> 
> So I assume Miscroft chnaged it's mind at some point in time, because
> obvious again, the referred called services aboove are Microsoft-
> written Windows service.

[followups trimmed to asm groups... pearls before swine y'know]

Yeah... it's different in Windows. A "hello world in a window" is as 
complicated as the X version, or worse. Something simpler will suffice. 
I can't find a Windows example that's "right" - "push -11" is *not* 
right, we're supposed to push -11 and call GetStandardHandle and use the 
return from *that* as a handle. This worked anyway, in win98, IIRC. 
It'll do...

; link /entry:start /subsystem:console hwin32.obj <wherever>\kernel32.lib

; you want this clutter in an include file, probably...

%define WriteFile _WriteFile@[EMAIL PROTECTED]
 ExitProcess _ExitProcess@[EMAIL PROTECTED]
     extern  WriteFile
     extern  ExitProcess

     global _start

section .text
            ; can put routines here, if you want
_start:

     push    dword 0          ; ??? unicode flag???
     push    dword num_chars  ; return value
     push    dword MSGLEN
     push    dword msg
     push    dword -11        ; can this be right???
     call    WriteFile

; callee "cleans up stack" - removes all those
; parameters we pushed, via "ret N"


exit_ok:
     push    dword 0
exit:
     call    ExitProcess

section .bss
     num_chars       resd   1

section .rodata
     msg             db      'Hello, Console.', 13, 10
     MSGLEN          equ     $ - msg
;----------------------------------

An approximate equivalent for Linux - this calls the kernel via the C 
calling convention, instead of the int 80h interface - but winds up 
executing exactly the same code, unless I'm mistaken...


; nasm -f elf hwcw.asm
; gcc hwcw.o -o hwcw
; ./hwcw

global main
extern write

section .data
     msg db 'Hello, World!', 10
     msg_len equ $ - msg
section .text
     main:

     push msg_len
     push msg
     push 1  ; stdout

     call write
     add esp, 12
; here we *do* have to "clean up stack"
; callee just ends in "ret"

     ret

Note that besides the different stack handling, Windows takes a 
parameter for the "return place" to return number of characters written, 
while Linux returns it in eax... When it comes to GUI apps, the Xwindows 
server is an app of its own - not part of the kernel at all. "I don't 
think we're in Kansas anymore, Toto!" :)

Best,
Frank
 




 22 Posts in Topic:
Calling libraries from assembler.
SoLo2 <spamtrap@[EMAI  2008-04-18 18:43:51 
Re: Calling libraries from assembler.
(Gordon Burditt) <spam  2008-04-18 23:22:54 
Re: Calling libraries from assembler.
Josef Moellers <spamt  2008-04-21 08:50:12 
Re: Calling libraries from assembler.
Frank Kotler <spamtra  2008-04-19 08:29:23 
Re: Calling libraries from assembler.
SoLo2 <spamtrap@[EMAI  2008-04-19 18:41:12 
Re: Calling libraries from assembler.
Terence <spamtrap@[EM  2008-04-20 15:33:52 
Re: Calling libraries from assembler.
Frank Kotler <spamtra  2008-04-21 00:33:57 
Re: Calling libraries from assembler.
santosh <spamtrap@[EM  2008-04-21 07:31:41 
Re: Calling libraries from assembler.
Robert Redelmeier <red  2008-04-21 02:04:02 
Re: Calling libraries from assembler.
Frank Kotler <spamtra  2008-04-21 05:25:02 
Re: Calling libraries from assembler.
"James Van Buskirk&q  2008-04-21 11:46:30 
Re: Calling libraries from assembler.
"robertwessel2@[EMAI  2008-04-20 19:20:38 
Re: Calling libraries from assembler.
Terence <spamtrap@[EM  2008-04-20 21:12:02 
Re: Calling libraries from assembler.
Frank Kotler <spamtra  2008-04-21 05:49:00 
Re: Calling libraries from assembler.
Robert Redelmeier <red  2008-04-21 14:25:53 
Re: Calling libraries from assembler.
Josef Moellers <spamt  2008-04-22 09:44:41 
Re: Calling libraries from assembler.
Josef Moellers <spamt  2008-04-22 09:42:38 
Re: Calling libraries from assembler.
Terence <spamtrap@[EM  2008-04-22 15:18:31 
Re: Calling libraries from assembler.
David Thompson <spamt  2008-05-05 04:10:33 
Re: Calling libraries from assembler.
"H. Peter Anvin"  2008-05-05 11:06:21 
Re: Calling libraries from assembler.
(Scott Lurndal) <spamt  2008-05-05 23:41:16 
Re: Calling libraries from assembler.
"H. Peter Anvin"  2008-05-12 23:44:56 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 2:18:31 CDT 2008.