flat assembler
Message board for the users of flat assembler.

Index > Main > Program works, but slowly (beginner question)

Author
Thread Post new topic Reply to topic
aviv0404



Joined: 07 Feb 2020
Posts: 5
aviv0404 07 Feb 2020, 08:32
Hey, I started learning assembly this week from the a udemy course that uses FASM for teaching assembly.

One of the assignments is to write a program that takes a number N and prints back all the pairs (x,y) such that x < y < N.

So first I wrote the program in python to make things easier:
[img]https://imgur.com/SDktZuB[/img]
Code:
n = 4
for i in range (n):
        for j in range(n):
                if (i < j):
                        if (j < n):
                                print ((i,j))

    



Then I wrote it in assembly following the python verison so my code won't be considered as "spaghetti code".
And it looks like this:
[img]https://imgur.com/iEvQZa9[/img]
Code:
;unsigned: JA JB JAE JBE
;signed: JL JG JLE JGE

format PE console
entry start

include 'win32a.inc'

section '.text' code readable executable

start:
        call    read_hex
        mov     edi, eax ; n
        mov     esi, 0h ;i for loop 1
        mov     ecx, 0h ;j for loop 2
main_loop:
        for_1:
                for_2:
                        if_1:
                                ;condition
                                cmp     esi, ecx
                                jae     end_if_1
                                if_2:
                                        ;condition
                                        cmp     ecx, edi
                                        jae     end_if_2
                                        ;print numbers
                                        mov     eax, esi
                                        call    print_eax       
                                        mov     eax, ecx
                                        call    print_eax
                                end_if_2:
                        end_if_1:
                        ;increasing j and checking if its the end of the loop
                        inc     ecx
                        cmp     edi, ecx
                        jnz     for_2
                end_for_2:
                ;increasing i and checking if its the end of the loop
                inc     esi
                cmp     edi, esi
                jnz     for_1
        end_for_1:

end_process:
        push    0
        call    [ExitProcess]

include 'training.inc'
    


The teacher's solution looks like this:
[img]https://imgur.com/myC7eRt[/img]
Code:
; 1. Write a program that takes the number n as input, and prints back all the
;    pairs (x,y) such that x < y < n.
;

format PE console
entry start

include 'win32a.inc'

; ===============================================
section '.text' code readable executable

;       Make a loop that loops from x to y and prints all combinations. Place that loop
;       inside another loop that goes from y to n and also prints all combinations.
start:
    call    read_hex

        mov     ebx, eax        ; save user input
        mov     edx, 1          ; y counter

loop_y:
        mov     ecx, 0          ; x     counter

loop_x:
print_x_y:
  mov   eax, ecx
        call    print_eax       ; print x

  mov   eax, edx
        call    print_eax       ; print y

next_x:
        inc     ecx

        cmp     ecx, edx
        jnz   loop_x            ; if x !== y

next_y:
        inc     edx

        cmp     edx, ebx
        jnz   loop_y            ; if y !== n

    ; Exit the process:
        push    0
        call    [ExitProcess]

include 'training.inc'

    


The logic of my code works. but it's a lot slower than the teacher's code.
When I assemble my code and run it, it prints immediately the first set of numbers (first loop of for_2), but then the next loops are delayed (it also seems like the delay is of the same interval).

Am I using too many labels? Or did I end up really writing a spaghetti code? I don't see my loop getting stuck anywhere for any reason... I'd appreciate any help!

For example for 4 as the input ill get:
0
1
0
2
0
3
delay for some reason (1~3 seconds)
1
2
1
3
delay
2
3
delay
end
Post 07 Feb 2020, 08:32
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20339
Location: In your JS exploiting you and your system
revolution 07 Feb 2020, 09:20
If we rewrite the python code then you might see the problem:
Code:
n = 4
for i in range (n):
        for j in range(i+1,n):
                print ((i,j))    
You don't need any "if" statements. Start the j loop from the i value.
Post 07 Feb 2020, 09:20
View user's profile Send private message Visit poster's website Reply with quote
aviv0404



Joined: 07 Feb 2020
Posts: 5
aviv0404 07 Feb 2020, 11:48
revolution wrote:
If we rewrite the python code then you might see the problem:
Code:
n = 4
for i in range (n):
        for j in range(i+1,n):
                print ((i,j))    
You don't need any "if" statements. Start the j loop from the i value.


Alright so I rewrote it and it's smooth now
Code:
;unsigned: JA JB JAE JBE
;signed: JL JG JLE JGE

format PE console
entry start

include 'win32a.inc'

section '.text' code readable executable

start:
        call    read_hex
        mov     edi, eax ; n
        mov     esi, 0h ;i for loop 1
        mov     ecx, 0h ;j for loop 2
main_loop:
        for_1:
                mov     ecx, esi
                inc     ecx
                for_2:
                        mov     eax, esi
                        call    print_eax       
                        mov     eax, ecx
                        call    print_eax
                        ;increasing j and checking if its the end of the loop
                        inc     ecx
                        cmp     edi, ecx
                        jg              for_2
                end_for_2:
                ;increasing i and checking if its the end of the loop
                inc     esi
                cmp     edi, esi
                jg              for_1
        end_for_1:

end_process:
        push    0
        call    [ExitProcess]

include 'training.inc'
    

I still don't understand why the delay happened when the conditions where part of the code though
Post 07 Feb 2020, 11:48
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20339
Location: In your JS exploiting you and your system
revolution 07 Feb 2020, 12:18
The problem is that you didn't reset the ECX counter inside loop1. So ECX spins all the way to 2^32 before wrapping back to zero.
Post 07 Feb 2020, 12:18
View user's profile Send private message Visit poster's website Reply with quote
aviv0404



Joined: 07 Feb 2020
Posts: 5
aviv0404 07 Feb 2020, 13:13
revolution wrote:
The problem is that you didn't reset the ECX counter inside loop1. So ECX spins all the way to 2^32 before wrapping back to zero.


Oh now I see it!
The first loop was fine, ecx got to 4 and exit the loop but at the second loop it increased to 5 and then like you said there was a wraparound

Thanks a lot! I took this concept for granted since I'm used to high level languages like python. I'll definitely pay attention to this in the future!
Post 07 Feb 2020, 13:13
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20339
Location: In your JS exploiting you and your system
revolution 07 Feb 2020, 13:21
I highly recommend the use of a debugger. Especially a GUI debugger like Ollydbg. For beginners they can make things a lot clearer as you watch the execution process in action.
Post 07 Feb 2020, 13:21
View user's profile Send private message Visit poster's website Reply with quote
aviv0404



Joined: 07 Feb 2020
Posts: 5
aviv0404 07 Feb 2020, 13:55
revolution wrote:
I highly recommend the use of a debugger. Especially a GUI debugger like Ollydbg. For beginners they can make things a lot clearer as you watch the execution process in action.


Yeah a debugger will be really handy, I'll download Ollydbg right now!
Thank you once more.
Post 07 Feb 2020, 13:55
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 07 Feb 2020, 14:05
You can find some basic tips on how to work with fasmw + OllyDbg in my video guides.
Post 07 Feb 2020, 14:05
View user's profile Send private message Visit poster's website Reply with quote
aviv0404



Joined: 07 Feb 2020
Posts: 5
aviv0404 07 Feb 2020, 14:42
Tomasz Grysztar wrote:
You can find some basic tips on how to work with fasmw + OllyDbg in my video guides.


Thanks, I'll definitely look into it!
Post 07 Feb 2020, 14:42
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.