flat assembler
Message board for the users of flat assembler.
Index
> DOS > procedure... |
Author |
|
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. |
|||
03 Feb 2008, 17:32 |
|
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.
|
|||
03 Feb 2008, 17:35 |
|
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 |
|||
03 Feb 2008, 18:35 |
|
abuashraf 04 Feb 2008, 00:41
thank you guys
|
|||
04 Feb 2008, 00:41 |
|
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"
|
|||
04 Feb 2008, 15:23 |
|
bitRAKE 04 Feb 2008, 23:35
Thanks for noticing the error! Correction made above.
(I often type crap code without testing it.) |
|||
04 Feb 2008, 23:35 |
|
Picnic 06 Feb 2008, 17:58
You can also use PROC16.INC ported by LocoDelAssembly. It's working.
|
|||
06 Feb 2008, 17:58 |
|
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).
|
|||
06 Feb 2008, 18:05 |
|
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
_________________ Pure Assembly Language Funs |
|||||||||||||||||||||
06 May 2008, 12:13 |
|
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 _________________ Bug Nr.: 12345 Title: Hello World program compiles to 100 KB !!! Status: Closed: NOT a Bug |
|||
07 May 2008, 07:30 |
|
LocoDelAssembly 07 May 2008, 19:03
Quote:
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 |
|||
07 May 2008, 19:03 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.