flat assembler
Message board for the users of flat assembler.

Index > Main > Simple code went crashing

Author
Thread Post new topic Reply to topic
StrenoJr



Joined: 13 Mar 2014
Posts: 22
Location: Slovakia
StrenoJr 14 Mar 2014, 18:37
Hello, I am new here and don' know where to post a message requesting help.
I discovered fasm a day or two ago and I'm not a pro in asm at all so please help me.
Code:
format PE console
entry start

include 'win32a.inc'

section '.text' code executable
start:
        mov ax, 50;
        mov ebx, 0xA;
        xor cx, cx;
        change:
                xor edx, edx;
                div ebx;
                add edx, 49d;
                push edx;
                inc cx;
                cmp eax, 0;
                jne change;
        move:
                pop eax;
                mov [edi], al;
                add edi, 1;
                loop move;
        end:
        push 0;
        call [ExitProcess];

section '.rdata' data readable
        hello db 'Hello World!', 10, 0; I know, not used, but it was for the past code and I forgot to remove it

section '.idata' data readable import
        library kernel32, 'kernel32.dll', \
                msvcrt,   'msvcrt.dll'
        import kernel32, ExitProcess, 'ExitProcess'
        import msvcrt, printf, 'printf'    

I copied all the code just to create a simple application to be able to test stuff.
I don't know if it is correct but without the code beginning at "change" it runs without errors.
This code compiles fine but crashes on run.
It should be a int to string script moving a number from eax to the space defined at edi.
Thanks for any help, it drives me crazy.
Post 14 Mar 2014, 18:37
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 14 Mar 2014, 18:56
You must initialize edi to some valid address, before trying to "mov [edi], al".
Post 14 Mar 2014, 18:56
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
StrenoJr



Joined: 13 Mar 2014
Posts: 22
Location: Slovakia
StrenoJr 14 Mar 2014, 19:06
Could you give me a code on that?
Mine with define double continues with crashes.
Post 14 Mar 2014, 19:06
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1671
Location: Toronto, Canada
AsmGuru62 14 Mar 2014, 19:16
Also, issue with LOOP. It uses full ECX, but your code works with CX only.
May cause trouble if high bits of ECX are not zero.
Your code will LOOP much longer than intended.

P.S. Same with AX vs. EAX.
When you use DIV - CPU divides EDX:EAX, but the high bits of EAX may be undefined (50 was moved into AX, so high bits are left unused).

P.P.S. To define buffer with string:
Code:
buffer rb 32  ; buffer of 32 CHARs
...
mov edi, buffer   ; before converting the value
    


P.P.P.S. Also, if you planning to use the string somewhere - I think you must terminate it with 00h byte.
After LOOP is completed -- ECX is zero, so simply store CL at EDI:
Code:
mov [edi], cl
    
Post 14 Mar 2014, 19:16
View user's profile Send private message Send e-mail Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 14 Mar 2014, 19:30
SysWorm wrote:
Could you give me a code on that?
Mine with define double continues with crashes.

Something like this (read the comments):
Code:
format PE console
; There is no need to end the instructions with semicolon. 
; The semicolon ";" in assembly starts a comment.
start:
        mov eax, 50   ;   AVOID USING 16 bit registers in 32bit program.
        mov ebx, 0xA
        xor ecx, ecx   
        change:
                xor edx, edx
                div ebx
                add edx, '0'  ; This is more readable in FASM. And should be 48="0"
                push edx
                inc ecx;      ; USE ECX HERE.
                cmp eax, 0
                jne change

                mov  edi, MyBuffer    ; INIT EDI

        move:
                pop eax
                mov [edi], al
                add edi, 1
                loop move
                
                mov byte [edi], 0   ; don't forget to end your string with 0.

        end:
        push 0
        call [ExitProcess]

section '.rdata' data readable writeable
  MyBuffer  rb 256   ; BUFFER of 256 bytes.    

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9
Post 14 Mar 2014, 19:30
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
StrenoJr



Joined: 13 Mar 2014
Posts: 22
Location: Slovakia
StrenoJr 15 Mar 2014, 08:50
Guys thank you very much.
I removed everything and made it a second time and now it works like a charm.
I noticed also a fancy thing: not only writeable data is valid but writable data seems to be legal too.
*Don't know how I could put 49 instead of 48, I was probably stressed of it so much haha same as the size of registers was that bad because I didn't know what's wrong in there so I tried everything
Post 15 Mar 2014, 08:50
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 15 Mar 2014, 08:54
SysWorm wrote:
I noticed also a fancy thing: not only writeable data is valid but writable data seems to be legal too.
You can see in the file TABLES.INC the following:
Code:
symbols_8:
 db 'linkinfo',19h,9
 db 'readable',19h,30
 db 'resource',1Ah,2
 db 'writable',19h,31
symbols_9:
 db 'shareable',19h,28
 db 'writeable',19h,31
    
So, yes, both writeable and writable can be used interchangeably.
Post 15 Mar 2014, 08:54
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:  


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