News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

print sum of numbers Using Masm

Started by moky, May 15, 2012, 04:38:24 AM

Previous topic - Next topic

moky

this is a simple Assembly program. I am trying to output the Emp1 And Num1 in the following format:

Jow Blow

600 763 521

i get the following output:

Jow Blow

x

Also i want to output the total of the three numbers



.386
.MODEL FLAT

ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
Include io.h

.STACK  4096            ; reserve 4096-byte stack

.DATA                   ; reserve storage for data

Emp1        byte    'Jow Blow',13,10,0
Num1    DWORD   600,763,521



  .CODE                           ; start of main program code
  _start:
    mov     eax, Num1     ; first number to EAX
    add     eax, 158        ; add 158
    mov     sum, eax        ; sum to memory


output  Emp1
output  Num1





    INVOKE  ExitProcess, 0  ; exit with return code 0

PUBLIC _start                   ; make entry point public

  END                             ; end of source code

dedndave

Quote600 763 521

Also i want to output the total of the three numbers
;
;
Num1    DWORD   600,763,521

probably the simplest way to handle this is to split Num1 into 3 parts
you could also do it by using a bunch of code to split it up (dividing by 1000 twice)

the method you choose would depend on how many such numbers you will have
for one, or a few entries, i would split it up...
Num1a dd 600
Num1b dd 763
Num1c dd 521

"dd" is a shortcut - same as DWORD

moky

Thank you for your help dedndave

but is there a way to do it without splitting it 

qWord

Num1    DWORD   600,763,521
is the same as
Num1a dd 600
Num1b dd 763
Num1c dd 521

The difference is, that the second form allows access through names, while the first version would require something like this:
mov eax,Num1[0]
mov edx,Num1[4]
mov ecx,Num1[8]


You must convert the result sum into a string. The common method is to divide the number trough the radix (e.g. 10) until the result gets zero. The division's reminder are summed up with 0x30 (='0') and concatenated to a string, which can be printed.
FPU in a trice: SmplMath
It's that simple!

moky


dedndave

        .DATA

Num1    dd 600763521

        .DATA?

PartA   dd ?
PartB   dd ?
PartC   dd ?

        .CODE

start:  mov     ecx,1000

        mov     eax,Num1
        mov     edx,0
        div     ecx

;EAX = 600763
;EDX = 521

        mov     PartC,edx

        mov     edx,0
        div     ecx

;EAX = 600
;EDX = 763

        mov     PartB,edx
        mov     PartA,eax