flat assembler
Message board for the users of flat assembler.

Index > Main > FASMLIB - text to binary routines design

Author
Thread Post new topic Reply to topic
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 25 Oct 2006, 22:47
i am designing routine that reads number from text, and returns it as binary number.

question is: how to beheave if we are NOT on number. I can either return ZF=1, or return standard FASM error.

advantage of ZF-way is, that if caller wants to handle this, it's simple conditional jump agains complicated construct

disadvantage of ZF-way is, that caller always has to care about catching this case.

advantage of error is, that even if caller doesn't handle this case, he still ends up with "Awaing numeric value" error.

disadvantage is, that if caller wants to handle error (eg. provide info about line and column where the error happened), he has to create complicated construct.

Example:

With ZF way:
Code:
libcall text.read.hex2b, stdin
jc .error
jz .error_awaiting_number ;ZF set when we are NOT on number
;EAX = number here
...
...
.error_awaiting_number:
;print line and columnt number, name of document, etc. 
;this can be common for entire text reading
    
but if i decide this way, you ALWAYS need to have "jz" or "jnz" after every such routine.

With error way, this is the lazy option:
Code:
libcall text.read.hex2b, stdin
jc .error
;EAX = number here    
eg. if we don't care about much output, we just leave it this way and user gets at least "Awaiting numeric value".
If we want to handle error with this way, it's possible, but with more effort:
Code:
libcall text.read.hex2b, stdin
jnc @f
cmp eax, ERR_NOT_NUMERIC
je .error_awaiting_number
jmp .error
@@:    
or
Code:
libcall text.read.hex2b, stdin
jc .error_readnum
...
.error_readnum:  ;common for all number reading calls
cmp eax, ERR_NOT_NUMERIC
je .error_awaiting_numer
jmp .error    


soooo Question
Post 25 Oct 2006, 22:47
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 25 Oct 2006, 22:50
by the way, i am doing same with EOF (end of file) and OF flag. But in that case i HAVE to do it through flag, so no questioning there
Post 25 Oct 2006, 22:50
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
dead_body



Joined: 21 Sep 2005
Posts: 187
Location: Ukraine,Kharkov
dead_body 26 Oct 2006, 04:19
i am for the flag.
Post 26 Oct 2006, 04:19
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 26 Oct 2006, 05:27
a2f function i had wrote returns eax=0&ZF=1 on error, otherwise number in ST0 and pointer to the first byte after loaded number, so you can check is it valid terminator (like zero, space, tab or any other for specific situation)
Post 26 Oct 2006, 05:27
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 26 Oct 2006, 10:14
shoorick: i provided too few info. FASMLIB functions always return error wth CF=1/EAX=error code. my conversion routines beheave similar to what you described (except number is returned in EAX, and conversion length is stored in variable pointed by function argument).

But this operation is working with stream, eg. after you read number, next read (or peek) from stream shows you terminator character.

This is not issue, problem is whether to use flag (faster for lowlevel typing, less standardized) or return standard error (needs uglier code constructs, but is very "general").

I also forgot to mention one PRO of error: in future, it will be possible to very easily implement mass catching of errors, like in HLLs:
Code:
try
  libcall text.read
  libcall text.read
  libcall text.read
  ...
catch ERR_NOT_NUMERIC
  ;print position in text and error here
endtry    


i am not personally fan of try-catch, it forces you to write somewhat dumber than with "manual" handling of each call. But still it's nice, and someone might be interested.

so this question can be understood as higher-level vs. low-level
Post 26 Oct 2006, 10:14
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.