flat assembler
Message board for the users of flat assembler.

Index > DOS > My first FASM!!!!

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
2



Joined: 26 Sep 2006
Posts: 92
2 04 Nov 2006, 22:23
Hey,I just created my most efficient binary counting program!

Here is a different version that does the actual carrying manually.
Notice that it does not convert any number into binary. The ecx is only used as
a counter for how far it should count.

Code:
org 256

mov ah,9
xor ecx,ecx

p0:

mov dx,b
int 33

inc ecx
mov dl,1
mov bx,8

p1:
dec bx
mov dh,[b+bx]
and dh,dl
xor [b+bx],dl
mov dl,dh
cmp bx,0
ja p1

cmp ecx,255
jbe p0

int 32
b db 8 dup 48,13,10,36
    
Post 04 Nov 2006, 22:23
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 05 Nov 2006, 02:27
Save a byte (this is one of those well-known shortcuts):

Code:
83FB00        CMP     BX,0
85DB          TEST    BX,BX
    
Post 05 Nov 2006, 02:27
View user's profile Send private message Visit poster's website Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat 05 Nov 2006, 05:13
There's a lot more room for saving space than that... I got it down to 29 bytes
Code:
        org 100h
        mov ah,9
        mov dx,b
p0:     int 21h
        mov bx,b+8
p1:     dec bx
        xor [bx],bh
        jp p1
        jnp p0
        db 0c2h
b       db 8 dup 48,13,10,36
    

How it works:

  • the numbers are at 112h-119h, so BH is always 1 here
  • '0' has even parity and '1' has odd parity, so the inner loop continues carrying as long as it changed into a '0'
  • The final iteration will overflow the buffer, changing the C2 into C3 (ret), and the 7B (jnp) into 7A (jp), thus allowing the program to terminate.
Post 05 Nov 2006, 05:13
View user's profile Send private message Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2 05 Nov 2006, 10:38
Quote:

2, are you replacing every ADD with OR? note that it's not the same, it produce the same result only when the 1s bits have a 0 bit in the other operand, otherwise it produces wrong result.

Of course it's right in your case because 48 is 110000 and the numbers between 0 and 9 occupy only the four less significant bits.


Yep,I know that doesn't work all the time,but I think whoever made
ASCII intended to make it that way for a reason. It sure makes it easier.
I know all about the XOR swap. Even if it weren't the way it is now,with
binary,we only have 0 and 1. This means as long as we can swap
between them,it works.
Post 05 Nov 2006, 10:38
View user's profile Send private message Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2 05 Nov 2006, 10:45
Hey guys,I'm a little lost. I don't know anything about those parity jumps
or the test instruction. I am gonna look that up. I also don't quite get
how you terminated the program.

I definitely like that. You guys really must know how I like small stuff!
Post 05 Nov 2006, 10:45
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20516
Location: In your JS exploiting you and your system
revolution 05 Nov 2006, 11:53
2 wrote:
I like to use bitwise operations when possible. I read that
they are faster.
Sorry to be pedantic but ... add, sub, xor, or, and etc. all take the same time to execute in modern processors. All else being the same, there is no advantage to using one over the other in terms of time taken to decode or to execute.

I don't know where/when this myth started but I might be able to guess it goes back to the days when people were doing hardware adders with discrete transistors. Once the whole thing got into silicon and became a CPU then the add/sub/and/or/xor timing difference was no longer true. That must be about 30 years ago now. Amazing how some out of date information can still be floating around to catch people off guard.

Hope this helps.
Post 05 Nov 2006, 11:53
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 05 Nov 2006, 21:26
TEST is the same as AND, but it discards the result (just like CMP is to SUB). ANDing something against itself will only be zero (ZF) if the number is zero. Or something like that. Razz

P.S. You could also OR something against itself to test for zero.
Post 05 Nov 2006, 21:26
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 05 Nov 2006, 23:01
You can but is it advisable? suppose these codes
Code:
test    eax, eax
add     eax, edx
jz      .exit    
Code:
or      eax, eax
add     eax, edx
jz      .exit    


Both codes executes at same speed on a PPro class (or newer) CPU?
Post 05 Nov 2006, 23:01
View user's profile Send private message Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2 06 Nov 2006, 03:41
Quote:

TEST is the same as AND, but it discards the result (just like CMP is to SUB). ANDing something against itself will only be zero (ZF) if the number is zero. Or something like that. Razz

P.S. You could also OR something against itself to test for zero.


OK,so if I understand correctly,the TEST instruction acts as though it were
a single bit data type? It uses the ones bit or two operands.
The ZF zero flag has the result? I hope I get it. Tell me if I'm wrong.
Post 06 Nov 2006, 03:41
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20516
Location: In your JS exploiting you and your system
revolution 06 Nov 2006, 10:53
OR eax,eax
AND eax,eax
TEST eax,eax

They will all produce the same results and alter the EFLAGS register only.

CF=OF=0, ZF=set according to register, PF=set according to register, SF=set according to register

Although there is a minor speed benefit by using TEST. But the difference is so minor that unless you are writing a hyper-optimised alogrithm it won't be worth the worry. The reason is because with TEST the destination register does not get written back, this can help free up writeback resources and allow surrounding instructions to complete faster.
Post 06 Nov 2006, 10:53
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< 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.