flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Calculating the size of strings and program size?

Author
Thread Post new topic Reply to topic
wht36



Joined: 18 Sep 2005
Posts: 106
wht36
Hello everyone, sorry for asking, I am sure these questions have been answered before, I just can't quite find the right thread with my searches...

So how do I calculate the size of strings and program size?

Bascially I am trying to write a byte coder for nwscript (http://web.archive.org/web/20041021071359/www.torlack.com/index.html?topics=nwndata_ncs) using FASM's macroinstructions. However this is the first time I am trying to write my own macros...

For example
Code:
; macro to code bytes
macro CONSTS op1, op2, [string] ;Place Constant String Onto the Stack
{
    db 4,5 ; byte code for CONSTS in nwscript
    dd .... ; <---- size of string goes here
    db string
}

; header
db       "NCS V1.0B"
dd     .... ; <---- size of program goes here

    


I think I need to use $ - . or sizeof.xxx , but I am not quite sure how to use them... Please help!
Post 08 Nov 2008, 15:01
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
windwakr
for string size do something like
Code:
string db 'Hello, World!',0
stringsize dd $-string-1
    

The minus one makes up for the extra zero at end.

_________________
----> * <---- My star, won HERE
Post 08 Nov 2008, 15:26
View user's profile Send private message Reply with quote
wht36



Joined: 18 Sep 2005
Posts: 106
wht36
Thanks! But the size of string needs to be assigned before the actual string is defined though (kind of like PASCAL style string).
Post 08 Nov 2008, 15:37
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17492
Location: In your JS exploiting you and your system
revolution
maybe this:
Code:
; macro to code bytes
macro CONSTS op1, op2, [string] ;Place Constant String Onto the Stack
{
    local stringsize, stringstart
    db 4,5 ; byte code for CONSTS in nwscript
    dd stringsize ; <---- size of string goes here
    stringstart:
    db string
    stringsize=$-stringstart
}

; header
db      "NCS V1.0B"
dd      .... ; <---- size of program goes here     
Post 08 Nov 2008, 15:42
View user's profile Send private message Visit poster's website Reply with quote
wht36



Joined: 18 Sep 2005
Posts: 106
wht36
Thanks very much, I applied a similar thing to get the size of program, and tested out just now, worked very nicely (assembled and decompiled to same output). Thanks again!

One thing though, is it possible to make a macro for the header so that I can make the file into an include file? (instead of putting a label for the start and a constant to calculate the size at the end of each program)

For example
Code:
macro header {
   db      "NCS V1.0B"
   dd      .... ; <---- size of program goes here 
}
    
Post 08 Nov 2008, 16:08
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
wht36,

In short, no. How assembler supposed to know program size before it translates the source?
Post 08 Nov 2008, 16:17
View user's profile Send private message Reply with quote
wht36



Joined: 18 Sep 2005
Posts: 106
wht36
Sigh... I suppose not. I will probably have to make two macros, one to put at the start and the other at the end....
Post 08 Nov 2008, 16:48
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
wht36,

Beware of org, you may wish to redefine it too.
Post 08 Nov 2008, 17:05
View user's profile Send private message Reply with quote
wht36



Joined: 18 Sep 2005
Posts: 106
wht36
Thanks for your advice! I will keep it in mind and hope I don't need to redefine it.

For those who also play the game Neverwinter Nights, I've uploaded the bytecoder to http://nwvault.ign.com/View.php?view=Other.Detail&id=1357
Post 08 Nov 2008, 22: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 YouTube, Twitter.

Website powered by rwasa.