flat assembler
Message board for the users of flat assembler.

Index > DOS > procedure...

Author
Thread Post new topic Reply to topic
abuashraf



Joined: 11 Nov 2006
Posts: 88
abuashraf 03 Feb 2008, 13:34
Hi...

Could any one please give me a simple example with a littel explain
about procedure,how to call them and how to pass parameters to them,
may be procedure that adds two numbers will do the job Wink

please with simple explain.

Thanx.
Post 03 Feb 2008, 13:34
View user's profile Send private message Reply with quote
System86



Joined: 15 Aug 2007
Posts: 77
System86 03 Feb 2008, 17:32
Simple example of a subroutine:

Code:
;add value in dx to ax, return result in ax
add_numbers:
add ax, dx
ret
    


To use it, just put call add_numbers, with ax and dx containing the numbers you want to add.

Unlike in tasm/masm, there is no proc/end proc keywords, although you can use macros for these.
Post 03 Feb 2008, 17:32
View user's profile Send private message Reply with quote
System86



Joined: 15 Aug 2007
Posts: 77
System86 03 Feb 2008, 17:35
To pass parameters, you can either pass them in registers, in global variables, or on the stack. The above example uses registers.
Post 03 Feb 2008, 17:35
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4020
Location: vpcmpistri
bitRAKE 03 Feb 2008, 18:35
Code:
        format  MZ

        push 7654       ;
        push 3210       ;
        call far procs:AddAdd ;
;       ...


segment procs

AddAdd: push bp       ; preserve value
        mov bp,sp     ; setup frame
        mov ax,[bp-8] ; get first number
        add ax,[bp-6] ; add second number
        pop bp        ; restore old value
        retf 4        ; load CS:IP from the stack

;stack view SS:SP:
; +00 BP
; +02 IP - return instruction offset
; +04 CS - return segment
; +06 3210
; +08 7654    


Last edited by bitRAKE on 04 Feb 2008, 23:33; edited 1 time in total
Post 03 Feb 2008, 18:35
View user's profile Send private message Visit poster's website Reply with quote
abuashraf



Joined: 11 Nov 2006
Posts: 88
abuashraf 04 Feb 2008, 00:41
thank you guys
Post 04 Feb 2008, 00:41
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 04 Feb 2008, 15:23
The stack is not balanced on that code, either use retf 4 or use "add sp, 4" after "call far procs:AddAdd"
Post 04 Feb 2008, 15:23
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4020
Location: vpcmpistri
bitRAKE 04 Feb 2008, 23:35
Thanks for noticing the error! Correction made above.
(I often type crap code without testing it.)
Post 04 Feb 2008, 23:35
View user's profile Send private message Visit poster's website Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1389
Location: Piraeus, Greece
Picnic 06 Feb 2008, 17:58
You can also use PROC16.INC ported by LocoDelAssembly. It's working.
Post 06 Feb 2008, 17:58
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 06 Feb 2008, 18:05
But be careful, PROC16.INC is not meant for multi segment executables, you probably will have no problems with COM executables but with EXE ones like bitRAKE's example it will not work correctly (it will use retn instead of retf when you write ret and parameters addresses will be shifted by two bytes because it is not taking into account that CS is on the stack).
Post 06 Feb 2008, 18:05
View user's profile Send private message Reply with quote
Kevin_Zheng



Joined: 04 Jul 2003
Posts: 125
Location: China
Kevin_Zheng 06 May 2008, 12:13
Hi,Guys:
I've written some of macros (proc, endp, call) to support DOS, WIN32,WIN64 and Linux FASM program. You maybe retry it for this case.

Thanks.

You can find the macro package from as below web link:
http://board.flatassembler.net/topic.php?t=8213&postdays=0&postorder=asc&start=0

code example:
Code:
proc_far write_text str_seg,str_offset
   local local_proc:WORD
   push    ds
  mov     ax,[str_seg]
        mov     ds,ax
       mov     dx,[str_offset]
     mov     ah,9h
       int     21h
 mov     ax,write_char_n
     mov     [local_proc],ax
     mov     ax,'N'
    call    [local_proc],ax
     pop     ds
  ret
endp             
;----------------------------------------------------------------------------
proc       write_char_n char
   push    ds
  pusha
       mov     ax,cs
       mov     ds,ax
       mov     ah,2
        mov     dx,[char]
   xor     dh,dh
       int     21h
 popa
        pop     ds
        ret
endp       
    


Description:
Download
Filename: macro_extend100.rar
Filesize: 29.3 KB
Downloaded: 409 Time(s)

Description:
Download
Filename: dos_seg.asm
Filesize: 2.91 KB
Downloaded: 397 Time(s)


_________________
Pure Assembly Language Funs
Post 06 May 2008, 12:13
View user's profile Send private message MSN Messenger Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1900
DOS386 07 May 2008, 07:30
LocoDelAssembly wrote:
But be careful, PROC16.INC is not meant for multi segment executables, you probably will have no problems with COM executables but with EXE ones like bitRAKE's example it will not work correctly


DOS MZ EXE's don't have to use far call's/proc's/ret's if code fits into 64 KiB Wink

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 07 May 2008, 07:30
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 07 May 2008, 19:03
Quote:

DOS MZ EXE's don't have to use far call's/proc's/ret's if code fits into 64 KiB

And not for procs that are only intra-segment referenced neither, but some EXEs like the one provided by bitRAKE it will not work even if all the segments fits in a 64K block (or the memory segments are allocated with no gaps within? Even with that granted the call instruction is not "overloaded" to perform the required arithmetic to transform the far call in a near one).

And of course EXEs are not required to have a separate segment for every single thing, but as I have said, it will not work in MULTI-SEGMENT executables, not in COMs and single-segment EXEs Wink
Post 07 May 2008, 19:03
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.