flat assembler
Message board for the users of flat assembler.

Index > DOS > beginner - macro vs calls

Author
Thread Post new topic Reply to topic
dvlfrnd



Joined: 09 Mar 2007
Posts: 15
dvlfrnd
hi,
i have just started learning assembly,and i chose DOS for there seems to be less OS interference than the other platforms.
i haven't written anything in assembly yet,im just browsing other people's code at the moment to familiarize myself.
i have some questions:

about macros/calls:
i see some people have subroutines such as

label:
move x,y
.....
ret
..
..
call label

which i believe, a branch saves the program counter,sets it to the label's address,and returns back when done.

and some people use macros,this is just a text replacement of the code by fasm i guess.

1- would using macros rather than calls increase the memory usage? (longer code?)
2- is there a penalty for making calls? (a call would eat more cycles?)
3- where is the program counter stored when the program branches? (i.e how does a return call restore the program counter?)

about push/pop:
i've seen some people do:
push 0xa000
pop es

and others do:
mov ax,0xa000
mov es,ax

the second one looks like it is much more efficient,because the first one uses the stack,however in many examples i see people use push/pop, is there a particular reason for this?

about al,ah and ax:
are these the same?

mov ax,0x00
vs.
mov ax,0x0000

how do you achieve:
mov al,0x00
mov ah,0x00

by just using ax?


final:
did i get these right? please correct me if im on the wrong track

thank you very much, this looks like a very nice community,


Last edited by dvlfrnd on 09 Mar 2007, 15:55; edited 2 times in total
Post 09 Mar 2007, 14:19
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi
Too busy to answer to your questions, and I'm sure someone else will, but I suggest printing this to A4 http://smallcode.weblogs.us/wp-content/uploads/2006/05/CheatSheet.png
Post 09 Mar 2007, 14:26
View user's profile Send private message MSN Messenger Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
Most of your example are just the same has each other, and are sings and roundabouts as to which is best.
But you need to remember a jump (jmp), is a jump to label, with no return, a "call" would return.

PS: Once you have learnt some asm in dos, agood next step would be to code in DexOS, as it a like Dos, but with more memory (upto 4 GB) and 32bit pmode.


Last edited by Dex4u on 09 Mar 2007, 17:17; edited 1 time in total
Post 09 Mar 2007, 15:40
View user's profile Send private message Reply with quote
dvlfrnd



Joined: 09 Mar 2007
Posts: 15
dvlfrnd
okasvi & Dex4u , thank you for your directions,
jmp -> yes i realized i was using wrong terminology, i'll correct it
Post 09 Mar 2007, 15:52
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386


Nice, NOT only Win32, I just don't understand the "spin" loop Confused

Quote:
about macros/calls


Macros will be removed when assembling. Subs&calls remain.

Calls can slow down the program (usually negligible). Repeated usage of
large macros will waste memory, subs&calls are better then.

Quote:

mov ax,0x00
vs.
mov ax,0x0000


Exactly the same. mov ax,0 is good enough Shocked

Quote:

how do you achieve:
mov al,0x00
mov ah,0x00


mov ax,0 Shocked

Quote:

i have just started learning assembly,and i chose DOS


Smile

For 32-bit DOS programming you can use DPMI or DexOS, as Dex4u
pointed you, like he points everybody in this forum Wink

See also the FAQ's on the top of this subforum.

_________________
Bug Nr.: 12345

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

Status: Closed: NOT a Bug
Post 09 Mar 2007, 19:27
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
@NTOSKRNL_VXE, Thats my job, i am a DexOS spammer Laughing .

PS: I see your have done some of your own "DexOS spamming" Wink here:
http://www.drdosprojects.de/forum/drp_forum/posts/3601.html
Post 09 Mar 2007, 23:21
View user's profile Send private message Reply with quote
dvlfrnd



Joined: 09 Mar 2007
Posts: 15
dvlfrnd
Thank you NTOSKRNL_VXE,
I read that anything that alters the Instruction Pointer ends up dumping prefetched next instruction bytes from memory and cause you to lose cycles,however i am not sure if this applies to the latest intel processors.(must do more reading)
so i believe choosing a macro vs. call is about memory usage vs. speed, which makes sense when put in related context,and your program's needs.
I still couldn't figure out where the Instruction Pointer is stored when there is a call.
I've read about ARM assembly a little bit and in ARM,the programmer explicitly has to
move program counter to link register before the jump,then move link register back int o program counter on return.makes a lot of sense.it is obvious this is done implicitly in intel architecture,just curious how.
thanks for the help
Post 10 Mar 2007, 15:27
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
Quote:
I read that anything that alters the Instruction Pointer ends up dumping prefetched next instruction bytes from memory and cause you to lose cycles,however i am not sure if this applies to the latest intel processors.(must do more reading)
so i believe choosing a macro vs. call is about memory usage vs. speed


Considering from your beginnerous questions, you can safely ignore
such "problems" for now Laughing

Also, for DOS development, 80386 compatible code is rather useful
then chasing the last "wasted" picosecond on latest intel processors Laughing

Quote:
couldn't figure out where the Instruction Pointer is stored when there is a call.


On the stack. Shocked

_________________
Bug Nr.: 12345

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

Status: Closed: NOT a Bug
Post 10 Mar 2007, 20:39
View user's profile Send private message Reply with quote
dvlfrnd



Joined: 09 Mar 2007
Posts: 15
dvlfrnd
Thanks for the advice,i just like knowing details.

call -> pushes IP to the stack (and does related calculations)
ret -> pops stack and jump to the address

just like you said Very Happy
Post 11 Mar 2007, 02:02
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
Quote:

about push/pop:
i've seen some people do:
push 0xa000
pop es

and others do:
mov ax,0xa000
mov es,ax

the second one looks like it is much more efficient,because the first one uses the stack,however in many examples i see people use push/pop, is there a particular reason for this?


The first example doesn't overwrite AX, so that's more convenient, IMO. However, only the second runs on an 8086 (because pushing immediates is supported only on 80186+ ... not that most people care anymore, just FYI).

Quote:

about al,ah and ax:
are these the same?


AH (high, 8 bits) and AL (low, 8-bits) are the two halfs that compose AX (16-bits). Same goes for BX (BH/BL), CX (CH/CL), DX (DH/DL). I think long mode (AMD64/EM64T) has some other variants too.
Post 13 Mar 2007, 09:04
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7802
Location: Kraków, Poland
Tomasz Grysztar
rugxulo wrote:
The first example doesn't overwrite AX, so that's more convenient, IMO. However, only the second runs on an 8086 (because pushing immediates is supported only on 80186+ ... not that most people care anymore, just FYI).

Don't forget that the first one is shorter by one byte. For this reason it's used in the size competitions.
Post 13 Mar 2007, 09:40
View user's profile Send private message Visit poster's website Reply with quote
dvlfrnd



Joined: 09 Mar 2007
Posts: 15
dvlfrnd
thanks a lot,i've seen examples of al and ah being set cleverly in different segments of the code to achieve some ax value.
there's one more thing i'm curious about,
that is,how to dynamically allocate memory.
db directive looks static,as it adds itself up to the end of the executable,
how could one safely use memory space without causing harm to others?
Post 13 Mar 2007, 15:58
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi
Use stack or allocate memory from the heap.
Post 13 Mar 2007, 16:40
View user's profile Send private message MSN Messenger Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
dvlfrnd wrote:
db directive looks static,as it adds itself up to the end of the executable
Yes it's static but it doesn't necessarily add itself to the end.

Hint: you can write instructions only with dbs, provided you know the opcodes.
That's what i like about assembly: you have freedom. DB puts the specified bytes exactly where you use it. You can imagine instructions are just "macros" that use db to compile the bytes. Wink
Post 17 Mar 2007, 17:27
View user's profile Send private message Reply with quote
dvlfrnd



Joined: 09 Mar 2007
Posts: 15
dvlfrnd
The_Grey_Beast wrote:

Hint: you can write instructions only with dbs, provided you know the opcodes.
That's what i like about assembly: you have freedom. DB puts the specified bytes exactly where you use it. You can imagine instructions are just "macros" that use db to compile the bytes. Wink

That is very helpful,and a very interesting idea.
please correct me if i got it wrong:
if "mov ax,34h" was something like "ABCD34" in hex
instead of writing
Code:
...
mov ax,34h
...
    

i could also use
Code:
...
db ABh ; assuming that i won't need a label
db CDh
db 34h
...
    


that opens endless possibilities!

Thanks again Smile

i have a very simple question,
suppose i have a code that compiles to 16 bytes,
i add this line:
buffer db 4 dup (1)
the com file becomes 20 bytes
if i do this instead:
buffer db 4 dup (?)
com file's size remains 16 bytes.

i'm going to make an uneducated guess,and say,the executable will occupy 20 bytes of memory space,
if so,
how does this happen? (who allocates that space?)
edit - i realized it only happens if .....(?) expression is at the very end of the file


Last edited by dvlfrnd on 18 Mar 2007, 15:39; edited 1 time in total
Post 18 Mar 2007, 06:52
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
Quote:
if "mov ax,34h" something like "ABCD34" in hex


Wrong. MOV AX,xxxx is $B8 in use16. Operand has 2 bytes.

Code:
 db $B8,$34,0   ; mov ax,$34
    


Some old assemblers suffered from lack of new instructions so you
can see some code using db to code new instructions like
CPUID, RDTSC, RDMSR and WRMSR.

http://board.flatassembler.net/topic.php?t=6676

_________________
Bug Nr.: 12345

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

Status: Closed: NOT a Bug
Post 18 Mar 2007, 11:01
View user's profile Send private message Reply with quote
dvlfrnd



Joined: 09 Mar 2007
Posts: 15
dvlfrnd
i made those numbers up Smile
why is there a '0' on the tail?
thanks
Post 18 Mar 2007, 15:38
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
Quote:
why is there a '0' on the tail


Code:
db $B8,$12,$FF   ; mov ax,$FF12     


AX has 16 bits. Laughing

_________________
Bug Nr.: 12345

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

Status: Closed: NOT a Bug
Post 18 Mar 2007, 16:24
View user's profile Send private message Reply with quote
dvlfrnd



Joined: 09 Mar 2007
Posts: 15
dvlfrnd
NTOSKRNL_VXE wrote:

Code:
db $B8,$12,$FF   ; mov ax,$FF12     

AX has 16 bits. Laughing


i see,very clear now,you've also answered one of my future questions Wink
thanks NTOSKRNL_VXE Smile
Post 18 Mar 2007, 16:38
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.