flat assembler
Message board for the users of flat assembler.

Index > Windows > Output a number

Author
Thread Post new topic Reply to topic
Chaos90



Joined: 03 Mar 2011
Posts: 3
Location: -5
Chaos90
Hi,
So I am very new to all this, I mean just looked at in now and have reached a dead end. Here is my code:

Code:
include 'win32ax.inc'

.data
         a DD 3

.code

  start:
        mov EAX, a
        invoke  MessageBox,HWND_DESKTOP,EAX,NULL,MB_OK
        invoke  ExitProcess,0

.end star    


The only way to even get an output was to move a to EAX and pass EAX to the message box, I did try passing [a] but it came up with the program has stopped working. Passing [a] would give the value of a no??

The above code seems to pass EAX the address of a right? And the output is L =/
However if i change a DD 3 to a DD 'hello' or any string, it all works fine =/
If anyone could help, I bet it's a real simple answer, then that would be fantastic!

EDIT:
So why wouldn't this code work, the program just stops working.
Code:
include 'win32ax.inc'


.data
         a DD 3
         b DD 5
         c DD ?

.code

  start:
        mov EAX,[a]
        add EAX,[b]
        push EAX
        invoke  MessageBox,HWND_DESKTOP,EAX,NULL,MB_OK
        invoke  ExitProcess,0

.end start        


Also what are the chances I will get this done in a day or so? Take an expression such as a=b+c*d-e and output it?
Post 03 Mar 2011, 13:56
View user's profile Send private message AIM Address Reply with quote
ctl3d32



Joined: 30 Dec 2009
Posts: 204
Location: Brazil
ctl3d32
MessageBox expects a string, not a number.

Use this:

Code:
include 'win32ax.inc'

.data
         a DD 3
         b DD 5
         c DD ?
         fmt db '%d',0
         output_string rb 20h

.code

  start:
        mov EAX,[a]
        add EAX,[b]
        invoke  wsprintf,output_string,fmt,EAX
        invoke  MessageBox,HWND_DESKTOP,output_string,NULL,MB_OK
        invoke  ExitProcess,0

.end start
    
Post 03 Mar 2011, 14:57
View user's profile Send private message Reply with quote
Chaos90



Joined: 03 Mar 2011
Posts: 3
Location: -5
Chaos90
Ahh thank you that works though I am not sure how Smile
Where do you find all this information out? I just can't find any documentation on it anywhere so all these structures like message box I don't know what each part does!

Also how do I divide? For multiplication I use imul as mul just gives me and error but both div and idiv give me errors. But if you know of any good tutorials or guides on this stuff I would appreciate it greatly as answering all these noob questions is probably a waste of your time Smile

div EAX,[c]

gives me error:extra characters on line
Post 03 Mar 2011, 15:30
View user's profile Send private message AIM Address Reply with quote
ctl3d32



Joined: 30 Dec 2009
Posts: 204
Location: Brazil
ctl3d32
Google for "MessageBox" or "wsprintf" and you will get your answer.

Google is your best friend...
Post 03 Mar 2011, 16:17
View user's profile Send private message Reply with quote
ctl3d32



Joined: 30 Dec 2009
Posts: 204
Location: Brazil
ctl3d32
Code:
include 'win32ax.inc'

.data
         a DD 8
         b DD 2
         c DD 2
         fmt db '%d',0
         output_string rb 20h

.code

  start:
        mov EAX,[a]
        add EAX,[b]
        cdq
        div [c]
        invoke  wsprintf,output_string,fmt,EAX
        invoke  MessageBox,HWND_DESKTOP,output_string,NULL,MB_OK
        invoke  ExitProcess,0

.end start
    
Post 03 Mar 2011, 16:19
View user's profile Send private message Reply with quote
Chaos90



Joined: 03 Mar 2011
Posts: 3
Location: -5
Chaos90
Ahh okay that makes sense thanks.
Okay so I have been trying for the past 2 or 3 hours to get this to produce the right output.

Code:
include 'win32ax.inc' 

.data
         t0 DB 1
         t1 DB 2
         t2 DB 3
         fmt db '%d',0 
         output_string rb 20h 

.code 

  start:
        mov al, 6
        mul [t0]
        mov ah, [t1]
        cdq
        div [t2]
        add al, ah
        movzx EAX, al
        push EAX

        invoke  wsprintf,output_string,fmt,EAX
        invoke  MessageBox,HWND_DESKTOP,output_string,NULL,MB_OK 
        invoke  ExitProcess,0 

.end start      

That particular one produces 174 =/
al = 6 * 1
ah = 2/3
eax = al + ah = 9
unless my math is off?
Not sure if using movzx is causing this, I couldn't use mov as it didn't like trying to move the two different sizes.
What exactly does the 20h do for the output string, reserve 20...?
And changing t1 to anything higher than 2 causes it to just crash. Man where do people even start learning this language!

Thanks for the help, really appreciate it.
Post 03 Mar 2011, 21:56
View user's profile Send private message AIM Address Reply with quote
ctl3d32



Joined: 30 Dec 2009
Posts: 204
Location: Brazil
ctl3d32
You're using the register eax completely wrong. You can't do that.

Download OllyDbg. It's a debugger and will let you execute your code line by line and see wat's going on.

Code:
mov al,6  --> eax = 00000006h = 6
mul [t0]  --> eax = 00000006h = 6
mov ah,[t1] --> eax = 00000206h = 518
cdq
div [t2] --> eax = 000002ACh => AC = 172 = 518/3
add al,ah  --> eax = 000002AEh => AE = 174
movzx eax,al  --> eax = 000000AEh = 174
    
Post 03 Mar 2011, 23:09
View user's profile Send private message Reply with quote
ctl3d32



Joined: 30 Dec 2009
Posts: 204
Location: Brazil
ctl3d32
You should really read the .pdf documentation inside fasm folder. You will learn a lot. You can't do this try and error when you don't even have a clue of what you are doing.
Post 03 Mar 2011, 23:17
View user's profile Send private message Reply with quote
ctl3d32



Joined: 30 Dec 2009
Posts: 204
Location: Brazil
ctl3d32
Your math is wrong. 6 + 2/3 = 20/3 = 6.6666... ~ 7

I would use float point numbers:

Code:
include 'win32ax.inc'

.data

  t1            dd 2
  t2            dd 3
  t3            dd 6
  result        dd ?
  fmt           db '%d',0
  output_string rb 20h

.code 

  start:
        fild    dword [t3]
        fld1
        fmulp   st1,st0
        fild    dword [t1]
        fild    dword [t2]
        fdivp   st1,st0
        faddp   st1,st0
        fistp   dword [result]
        invoke  wsprintf,output_string,fmt,[result]
        invoke  MessageBox,HWND_DESKTOP,output_string,NULL,MB_OK 
        invoke  ExitProcess,0 

.end start
    
Post 03 Mar 2011, 23:33
View user's profile Send private message Reply with quote
ctl3d32



Joined: 30 Dec 2009
Posts: 204
Location: Brazil
ctl3d32
You can also do this:

Code:
include 'win32ax.inc'

.data

  t0            db 1
  t1            db 2
  t2            db 3
  result        dd ?
  fmt           db '%d',0
  output_string rb 20h

.code 

  start:
        push    ebx edx
        xor     eax,eax
        mov     al,6
        imul    [t0]
        imul    [t2]
        add     al,[t1]
        movzx   ebx,[t2]
        cdq
        idiv     ebx
        invoke  wsprintf,output_string,fmt,eax
        invoke  MessageBox,HWND_DESKTOP,output_string,NULL,MB_OK
        pop     edx ebx
        invoke  ExitProcess,0 

.end start
    
Post 03 Mar 2011, 23:58
View user's profile Send private message Reply with quote
pearlz



Joined: 07 Jun 2010
Posts: 55
Location: Viet Nam
pearlz
Code:
proc Int2Str uses eax ebx edx esi edi, intS, strO
   local tmp[TMP_MAX]: BYTE;
   MASK  =  1 shl 31
   mov   ebx,10
   mov   eax,[intS]
   lea   esi,[tmp+TMP_MAX-1]
   mov   edi,[strO]
   test  eax,MASK
   jz    process
   mov   byte [edi],'-'
   inc   edi
   not   eax
   inc   eax
   process:
     xor   edx,edx
     test  eax,eax
     jz    .done
     dec   esi
     div   ebx
     add   dl,'0'
     mov   [esi],dl
     jmp   process
    .done:
      lodsb
      stosb
      test   al,al
      jnz    .done
    ret
endp

proc Int2StrU uses eax ebx edx esi edi, intS, strO
  local tmp[TMP_MAX]: BYTE
  mov   ebx,10
  mov   eax,[intS]
  lea   esi,[tmp+TMP_MAX-1]
  .div:
     xor    edx,edx
     test   eax,eax
     jz     .done
     dec    esi
     div    ebx
     add    dl,'0'
     mov    [esi],dl
     jmp    .div
  .done:
     mov     edi,[strO]
  copy:
     lodsb
     stosb
     test   al,al
     jnz    copy
  ret
endp
    
Post 07 Mar 2011, 20:55
View user's profile Send private message Reply with quote
pearlz



Joined: 07 Jun 2010
Posts: 55
Location: Viet Nam
pearlz
Int2Str 'll convert 12345 -> '12345'
-12345 -> '-12345'
Int2StrU for unsigned int
've fun.
Post 07 Mar 2011, 20:58
View user's profile Send private message Reply with quote
pearlz



Joined: 07 Jun 2010
Posts: 55
Location: Viet Nam
pearlz
TMP_MAX >= 11 Very Happy
Post 07 Mar 2011, 21:00
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 YouTube, Twitter.

Website powered by rwasa.