News:

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

sub numbers larger than the capacity of the register

Started by mahmudkais, February 23, 2010, 10:59:00 AM

Previous topic - Next topic

mahmudkais

hi all
in masm 32 is there a way to sub numbers larger than the capacity of the registers
DB " 001400601200560",0
DB " 000000160020333",0

lets say that i want to convert the above numbers from ascii to hex and substract one from the other
how can i do that in masm32 with or without floating point doesn't matter
thanks

dedndave

first, you would not convert them to hex - you would convert them to binary
but, there are SUB (subtract) and SBB (subtract with borrow) instructions to perform multiple-precision math
you could do it with string math - but it isn't very fast
it depends on what form the values are in to begin with, and what form you want them in at the end
if you are going to convert them back to ASCII decimal, string math may be just as fast or faster for smaller values
to do it in binary, you have to convert the values to binary, then subtract, then convert them back to ASCII decimal
BCD might be a good compromise

http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_6/CH06-2.html#HEADING2-171

qWord

you can use the a2uq (asc to unsigned qword) or a2sq ( asc to signed qword) macros for conversion:
include masm32rt.inc
...
.data
szDq1 DB " 001400601200560",0
szDq2 DB " 000000160020333",0
.code
mov eax,a2uq(OFFSET szDq1) ; convert szDq1 to unsigned qword using sscanf
; eax = pointer to unsigned qword

mov edi,[eax] ; save qword to esi::edi
mov esi,[eax+4] ;

mov eax,a2uq(OFFSET szDq2) ; load second qword

add edi,[eax] ; add
adc esi,[eax+4] ;

;sub edi,[eax] ; sub
;sbb esi,[eax+4] ;

mov eax,edi
mov edx,esi
print uqword$(edx::eax) ; print result to console

inkey "..."
invoke ExitProcess,0
FPU in a trice: SmplMath
It's that simple!

FORTRANS

#3
Hi,

   At least four ways come to mind.  One, convert to binary
as Dave and qword mention, search on extended precision
math.  Two, convert to unpacked BCD and process the digits
with subtract and AAS instructions.  Three, convert to packed
BCD and process digits with subtract and DAS instructions.
Fourth, format the digits as a ten byte packed BCD number
and process in the FPU with FBLD, FSUB, and FBSTP instructions.
And I'm sure there are many more ways.

   An example:


        PAGE ,132
        TITLE   Subtract Numbers
        NAME    SUB015

        COMMENT *
23 February 2010, based on a thread on the MASM32 forum, do an
extended precision subtraction.
                *

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CODE    SEGMENT
        ASSUME  CS:CODE,DS:CODE,ES:NOTHING
        ORG     100H ; COM file opening
START:
        JMP     Start1

; Fifteen Digit Number Strings (MS-DOS Format).

Big1    DB      "001400601200560", "$"
Big2    DB      "000000160020333", "$"
BResult DB      "               ", "$"
Count   DW      15

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Start1:

;   Conversion of ASCII to BCD loop.
        MOV     SI,OFFSET Big1
        MOV     BX,OFFSET Big2
        MOV     CX,[Count]

Start2:
        AND     BYTE PTR [SI],0FH  ; Convert ASCII byte to BCD.
        AND     BYTE PTR [SI],0FH  ; Convert...
        INC     SI
        INC     BX
        LOOP    Start2

;   Point registers to the end of our number and result
; strings, set loop count, set string direction and clear carry.
        MOV     SI,OFFSET Big1+14
        MOV     BX,OFFSET Big2+14
        MOV     DI,OFFSET BResult+14
        MOV     CX,[Count]
        STD
        CLC

;   Subtraction loop.
Start3:
        LODSB           ; Get first number (BCD),
        SBB     AL,[BX] ; subtract,
        AAS             ; BCD adjustment,
        STOSB           ; and save result.
        DEC     BX      ; DEC does not affect carry.
        LOOP    Start3

;   Conversion of result BCD to ASCII loop.
        MOV     DI,OFFSET BResult
        MOV     CX,[Count]
        CLD

Start4:
        ADD     BYTE PTR [DI],'0'       ; Adding an ASCII zero
        INC     DI                      ; converts BCD to ASCII.
        LOOP    Start4

        MOV     DX,OFFSET BResult       ; Display result.
        MOV     AH,9
        INT     21H

        MOV     AH,4CH  ; DOS 2.0+ exit.
        INT     21H

CODE    ENDS
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        END START

mahmudkais

Thank you very much dedndave, qWord, FORTRANS,  For your fast replays
And thank you qWord, FORTRANS, for the examples
You are the best