The MASM Forum

General => The Workshop => Topic started by: ikudesnik on October 04, 2015, 01:32:07 AM

Title: How to convert a string to a signed number?
Post by: ikudesnik on October 04, 2015, 01:32:07 AM
How to convert a string to a signed number?
Title: Re: How to convert a string to a signed number?
Post by: ikudesnik on October 04, 2015, 01:33:43 AM
If you considered in line with the field EDIT

PUSH OFFSET string
PUSH 255
PUSH WM_GETTEXT
PUSH HWNDEDT2
CALL SendMessageA@16
Title: Re: How to convert a string to a signed number?
Post by: qWord on October 04, 2015, 03:13:03 AM
e.g. using the CRT function sscanf (https://msdn.microsoft.com/en-us/library/zkx076cy.aspx):
; x is SDWORD
; frmt db "%d",0
invoke crt_sscanf, ADDR string, ADDR frmt, ADDR x

In the MASM32 library there is also the function atol:
invoke atol,ADDR string
mov x,eax

...and a macro:
mov x, sval(ADDR string)
Title: Re: How to convert a string to a signed number?
Post by: jj2007 on October 04, 2015, 05:27:20 AM
Pure assembler :biggrin:

include \masm32\MasmBasic\MasmBasic.inc      ; download (http://masm32.com/board/index.php?topic=94.0)
  Init
  Inkey Str$("The decimal representation of your input is %i", Val(Prompt$("Type a decimal, hex or binary number, then close this window:", "0x123")))
  Exit
EndOfCode
Title: Re: How to convert a string to a signed number?
Post by: ikudesnik on October 04, 2015, 07:31:01 PM
In kernel32.inc not described functions atol, strtoint. What to do?
I Use MASM32.
Title: Re: How to convert a string to a signed number?
Post by: hutch-- on October 04, 2015, 07:55:46 PM
Use the recommended include file. The functions you refer to are in MSVCRT.

The include file is "masm32rt.inc".
Title: Re: How to convert a string to a signed number?
Post by: Vortex on October 04, 2015, 07:56:14 PM
QuoteIn kernel32.inc not described functions atol, strtoint.

atol is defined in \masm32\include\masm32.inc

About StrToInt :

https://msdn.microsoft.com/en-us/library/windows/desktop/bb773446%28v=vs.85%29.aspx
Title: Re: How to convert a string to a signed number?
Post by: dedndave on October 05, 2015, 06:19:55 AM
Quote from: ikudesnik on October 04, 2015, 01:33:43 AM
If you considered in line with the field EDIT

PUSH OFFSET string
PUSH 255
PUSH WM_GETTEXT
PUSH HWNDEDT2
CALL SendMessageA@16

we generally use INVOKE to do that for us....

    INVOKE  SendMessage,HWNDEDT2,WM_GETTEXT,255,offset string

if you start your program with this single line,
it will add most of the inc's and lib's you will need

        INCLUDE    \Masm32\Include\Masm32rt.inc

i often add these lines after that one, to enable newer instructions
        .686p
        .MMX
        .XMM
Title: Re: How to convert a string to a signed number?
Post by: jj2007 on October 05, 2015, 08:36:01 AM
Quote from: ikudesnik on October 04, 2015, 01:33:43 AM
If you considered in line with the field EDIT

PUSH OFFSET string
PUSH 255
PUSH WM_GETTEXT
PUSH HWNDEDT2
CALL SendMessageA@16

I see that nobody here honours my fantastic Val (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1202) macro :(

So, here is a plain WinAPI solution: GetDlgItemInt.
Title: Re: How to convert a string to a signed number?
Post by: rrr314159 on October 05, 2015, 10:33:43 AM
Quote from: jj2007I see that nobody here honours my fantastic Val macro :(

- somebody once advised "cast not your pearls before swine" :biggrin: