Author Topic: Adding two numbers with MASM32 ?  (Read 27241 times)

NoCforMe

  • Member
  • *****
  • Posts: 1043
Re: Adding two numbers with MASM32 ?
« Reply #60 on: July 27, 2022, 07:26:09 PM »
So now do it where you get those numbers from the user (via text input). How would you do that?

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10314
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Adding two numbers with MASM32 ?
« Reply #61 on: July 27, 2022, 07:31:43 PM »
I guess that the only problem with code of this type,

printf  ("\nResult: %u\n", eax)

is you pull in the C runtime library and unless you are calling MSVCRT, your app just got a lot bigger.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

daydreamer

  • Member
  • *****
  • Posts: 2307
  • my kind of REAL10 Blonde
Re: Adding two numbers with MASM32 ?
« Reply #62 on: July 27, 2022, 08:38:10 PM »
alternative to read in string->convert to dword ->add ->convert to ascii
is perform a BCD add
http://www.ray.masmcode.com/BCDtut.html
my none asm creations
http://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

jj2007

  • Member
  • *****
  • Posts: 13657
  • Assembly is fun ;-)
    • MasmBasic
Re: Adding two numbers with MASM32 ?
« Reply #63 on: July 27, 2022, 09:10:12 PM »
I guess that the only problem with code of this type,

printf  ("\nResult: %u\n", eax)

is you pull in the C runtime library and unless you are calling MSVCRT, your app just got a lot bigger.

Code: [Select]
include \masm32\include\masm32rt.inc

.code
start:
  mov eax, 12345
  invoke crt_printf, chr$("eax=%i"), eax
  exit
end start

1024 bytes. What do you mean with "pull in the C runtime library"?

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10314
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Adding two numbers with MASM32 ?
« Reply #64 on: July 27, 2022, 09:20:12 PM »
 :biggrin:

> unless you are calling MSVCRT, your app just got a lot bigger.

C runtime library is used by C, a static library.  :biggrin:
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

TimoVJL

  • Member
  • *****
  • Posts: 1258
Re: Adding two numbers with MASM32 ?
« Reply #65 on: July 28, 2022, 01:20:17 AM »
:biggrin:

> unless you are calling MSVCRT, your app just got a lot bigger.

C runtime library is used by C, a static library.  :biggrin:
masm32/64 have own static runtime library.
C don't always need crt and can work with bare win32 API and use Windows OS msvcrt.dll directly.
May the source be with you

NoCforMe

  • Member
  • *****
  • Posts: 1043
Re: Adding two numbers with MASM32 ?
« Reply #66 on: July 28, 2022, 09:46:30 AM »
Code: [Select]
include \masm32\include\masm32rt.inc

.code
start:
  mov eax, 12345
  invoke crt_printf, chr$("eax=%i"), eax
  exit
end start

1024 bytes. What do you mean with "pull in the C runtime library"?

Doesn't the "crt" in crt_printf() mean "C runtime"? (I know it's part of the MASM32 library.)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10314
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Adding two numbers with MASM32 ?
« Reply #67 on: July 28, 2022, 10:12:44 AM »
Timo is right, C can use either its native runtime, static libraries or it can call MSVCRT but when I see "printf" in masm code, I reasonably assume that its the C runtime, not the system DLL.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

NoCforMe

  • Member
  • *****
  • Posts: 1043
Re: Adding two numbers with MASM32 ?
« Reply #68 on: July 28, 2022, 10:32:23 AM »
I've always used wsprintf(), which is a Win32 function.

daydreamer

  • Member
  • *****
  • Posts: 2307
  • my kind of REAL10 Blonde
Re: Adding two numbers with MASM32 ?
« Reply #69 on: July 28, 2022, 08:38:05 PM »
I've always used wsprintf(), which is a Win32 function.
I always use masm32 "print" macro until recently I converted to GUI app with Catstr$ macro followed by invoke SendMessageA and/or invoke MessageBox...
very useful when simulating lots of prints to a big text GUI control


my none asm creations
http://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

NoCforMe

  • Member
  • *****
  • Posts: 1043
Re: Adding two numbers with MASM32 ?
« Reply #70 on: July 29, 2022, 09:12:35 AM »
wsprintf()/MessageBox() are very useful for inline debugging.

Can't tell you how many times I've done something like this:

Code: [Select]
.data

BuffPtrFmt DB "The buffer pointer is %08X", 0

.code

Something PROC
LOCAL buffPtr:DWORD, buffer[256]:BYTE

... do some stuff w/pointer ...

... now we want to see where the pointer is pointing:

INVOKE wsprintf, ADDR buffer, OFFSET BuffPtrFmt, buffPtr
INVOKE MessageBox, NULL, ADDR buffer, NULL, MB_OK

I've got all that stuff memorized ...

jj2007

  • Member
  • *****
  • Posts: 13657
  • Assembly is fun ;-)
    • MasmBasic
Re: Adding two numbers with MASM32 ?
« Reply #71 on: July 29, 2022, 09:53:06 AM »
wsprintf()/MessageBox() are very useful for inline debugging.

Can't tell you how many times I've done something like this:

I can tell you how often I use deb :tongue:

deeR44

  • Member
  • **
  • Posts: 101
Re: Adding two numbers with MASM32 ?
« Reply #72 on: July 29, 2022, 02:47:33 PM »
See this file in a later post as a 'zip' file.
« Last Edit: August 04, 2022, 12:23:27 PM by deeR44 »

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10314
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Adding two numbers with MASM32 ?
« Reply #73 on: July 29, 2022, 03:16:16 PM »
Just do it !  :biggrin:

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm64\include64\masm64rt.inc

    .code

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

 entry_point proc

    LOCAL pbuf1  :QWORD
    LOCAL buff1[128]:BYTE
    LOCAL pbuf2  :QWORD
    LOCAL buff2[128]:BYTE
    LOCAL var1  :QWORD
    LOCAL var2  :QWORD

    mov pbuf1, ptr$(buff1)
    mov pbuf2, ptr$(buff2)

    conout "Enter an integer "
    rcall StdIn,pbuf1,128

    conout "Enter an integer "
    rcall StdIn,pbuf2,128

    mov var1, uval(pbuf1)
    mov var2, uval(pbuf2)

    mov rax, var2
    add var1, rax

    conout "The total of both integers = ",str$(var1),lf

    waitkey
    .exit

 entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    end
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10314
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Adding two numbers with MASM32 ?
« Reply #74 on: July 29, 2022, 06:44:51 PM »
Floating point version.  :biggrin:

; «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»

    include \masm64\include64\masm64rt.inc

    .code

; «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»

 entry_point proc

    LOCAL pbuf1  :QWORD
    LOCAL buff1[128]:BYTE
    LOCAL pbuf2  :QWORD
    LOCAL buff2[128]:BYTE
    LOCAL var1  :REAL8
    LOCAL var2  :REAL8
    LOCAL rslt  :REAL8

    mov pbuf1, ptr$(buff1)                          ; get the pointer to the buffer
    mov pbuf2, ptr$(buff2)                          ; get the pointer to the buffer

    conout "Enter a floating point number "
    rcall StdIn,pbuf1,128                           ; get a floating point format string

    conout "Enter a floating point number "
    rcall StdIn,pbuf2,128                           ; get a floating point format string

    rcall atofp,pbuf1,ptr$(var1)                    ; convert string data to floating point
    rcall atofp,pbuf2,ptr$(var2)                    ; convert string data to floating point

    movsd xmm0, var1                                ; load fp var into xmm0
    movsd xmm1, var2                                ; load fp var into xmm1
    addsd xmm0, xmm1                                ; add the two together
    movsd rslt, xmm0                                ; copy result into fp var

    rcall fptoa,rslt,pbuf1                          ; reuse pbuf1

    conout "Floating point result = ",pbuf1,lf      ; display total

    waitkey
    .exit

 entry_point endp

; «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»

    end
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: