flat assembler
Message board for the users of flat assembler.

Index > DOS > Tutorial: Convert nibble to decimal

Author
Thread Post new topic Reply to topic
Ciper



Joined: 01 Jan 2013
Posts: 27
Ciper
Code:
org 100h
mov al,0ch       ; Nibble to convert
aaa   
or ax,3030h  
mov bx,ax
mov ah,2
rept 2 {
    mov dl,bh
    int 21h
    shl bx,8
}

int 20h
    

_________________
Ciper
Post 05 Jan 2013, 18:25
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Ciper,

Probably you've forgot about auxiliary carry (AF). Arithmetic instructions (add/cmp/sub etc.) give it meaningful, but not always zero, value; most other instructions either leave it unchanged (i.e. propagate it further) or have it undefined.

There is twice as long aam instruction, which splits al as you want. It accepts al up to 99, and immediate operand too (for weird radices and fun, and profit). Wink

shl bx, 8 is definitely an overkill, you need only mov bh, bl. If rept is eliminated, mov dl, bl is even better.

As a side note, ret is as good as int 20h for such short .COM programs (as you may already know why).

Some modifications lead to this:
Code:
        org     100h
        mov     al, 99  ; Number to convert (<100)
        aam
        or      ax, '00'
        mov     dl, ah
        mov     dh, al  ; int21/02 trashes al
        mov     ah, 2
        int     21h
        mov     dl, dh
        int     21h
        ret    
Do you have to pay for each space character being used, or maybe you're buying them in bulk and too greedy to spend some (for indentation, e.g.)? Wink
Post 05 Jan 2013, 19:25
View user's profile Send private message Reply with quote
Ciper



Joined: 01 Jan 2013
Posts: 27
Ciper
baldr,

Thank you very much for your constructive criticism and code+explanations; I am sure I will benefit from those (as I think some other readers as well).

ps: No excuse for bad space usage Sad

_________________
Ciper
Post 05 Jan 2013, 19:41
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Ciper,

Those spaces are just elements of style. Examine others' sources, find style that pleases you most, adjust it as you want, and use it properly. Be ready for criticism too, nobody's perfect. Wink

Heh, in your Principles of Operation thread you call FASM source "highly readable and annotated". Was the pun intended? Wink
Post 05 Jan 2013, 20:05
View user's profile Send private message Reply with quote
Ciper



Joined: 01 Jan 2013
Posts: 27
Ciper
baldr,

Sure; mea culpa; I treated these small tutorials with some neglect.

Some pun intended, indeed... But overall it -- FASM source -- is readable and modifyable.

By the way, what I especially like in your code is that '00'; sometimes we think too much in hex Wink

_________________
Ciper


Last edited by Ciper on 05 Jan 2013, 21:05; edited 3 times in total
Post 05 Jan 2013, 20:14
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 22:05; edited 1 time in total
Post 05 Jan 2013, 20:51
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Ciper,

If we do read in hex and can write in hex, how do anybody suppose that we don't think in hex? Wink

Long time ago I thought that Tomasz feed real sources to some stripper (no pun, some program like strip(1), only with other targets) that removes all comments, makes structure offsets plain numbers and such... but later some bugs discovered (ahoy, revolution!) convince me that those sources are appear authentic. They're readable (somewhat) and modifiable (to that extent, too).

Anyway, I don't think that 3Bh is any better than ';'. To paraphrase my mother, "never use constant of other type if you really mean it of this type".

----8<----
HaHaAnonymous,

No space after comma in operands. That makes me really mad. Wink
Tabs seem to be at 3. Not a power of 2 (or multiple of it).
Data definition directives are too far to right to be immediately recognized.

Not to mention (I don't think it's production code) those jmps to the next instruction's address. Wink
Post 05 Jan 2013, 21:12
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 22:05; edited 1 time in total
Post 05 Jan 2013, 21:24
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
HaHaAnonymous,

Space after comma is just a preference (you do have space after comma in your regular text, aren't you?).

Spaces instead of tabs are good. Only their quantity make me mad (or maybe, divisibility of their quantity?)
Code:
;    <----------------->
data1                   dd   $00000000      ; line comments at col. 45    
I mean exactly that.
Post 05 Jan 2013, 22:09
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 22:05; edited 1 time in total
Post 05 Jan 2013, 22:50
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
HaHaAnonymous,

Maybe it can relieve our eyes if you just tab db (or space it to line up) for short labels, and use one space after every label that extends out of tab space.

I do understand that you may have no problems with tracking those spaces. I don't understand why do you impose such abilities on us. Do you want us to read your code, or just to ignore it because you ignore its readability?

When I scan the foreign code, I don't have extra time (hell, I don't have it anyway). Well-formed code (not so strict as well-formed XML Wink) passes fast, unscrupulous and/or malformed code makes me stumble and think "why should I?"
Post 05 Jan 2013, 23:57
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.