News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Adding two numbers with MASM32 ?

Started by sunshine33, August 03, 2018, 08:01:30 AM

Previous topic - Next topic

NoCforMe

So now do it where you get those numbers from the user (via text input). How would you do that?
Assembly language programming should be fun. That's why I do it.

hutch--

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.

daydreamer

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
https://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

Quote from: hutch-- 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.

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

 :biggrin:

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

C runtime library is used by C, a static library.  :biggrin:

TimoVJL

Quote from: hutch-- 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:
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

Quote from: jj2007 on July 27, 2022, 09:10:12 PM
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.)
Assembly language programming should be fun. That's why I do it.

hutch--

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.

NoCforMe

I've always used wsprintf(), which is a Win32 function.
Assembly language programming should be fun. That's why I do it.

daydreamer

Quote from: NoCforMe on July 28, 2022, 10:32:23 AM
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
https://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

wsprintf()/MessageBox() are very useful for inline debugging.

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


.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 ...
Assembly language programming should be fun. That's why I do it.

jj2007

Quote from: NoCforMe 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:

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

deeR44

#72
See this file in a later post as a 'zip' file.

hutch--

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

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