Вопрос

How to use large numbers in? like 3441881739,30000000 etc

mov eax,3441881739

In this case eax value is a negative number. How to fix it? split it? how? I need to do also add/sub/mul/div etc the cmp operation.

Can someone explain and give an example how to do it?

I marked fasm and nasm tags but anothers assembly are welcome too. I'm on 32-bit machine.

Это было полезно?

Решение 2

If you want to deal with numbers larger than 232-1, you'll need to do multiple-precision arithmetic. For example, to add a pair of 64-bit numbers you could do something like this:

mov eax, A_1    ; essentially C = A + B
mov edx, B_1
add eax, edx    ; note that for the low word, we just add
mov C_1, eax
mov eax, A_2
mov edx, B_2
adc eax, edx    ; but for the high word, we add with carry
mov C_2, eax

Having access to the carry bit means that this is considerably simpler than with most higher level languages. There's also a subtract with borrow to make multi-word subtraction easy as well. Multiplication and division depend a little. A normal 32x32 bit multiplication produces a 64-bit result (low word in eax, high word in edx). If you want something like 64x64 bit multiplication (giving a 128-bit result) you'll need to implement that yourself.

For example, multiplication (without using the built-in mul/imul instructions) can be done something like this:

mult proc
; multiplies eax by ebx and places result in edx:ecx
    xor ecx, ecx
    xor edx, edx
mul1:
    test ebx, 1
    jz  mul2
    add ecx, eax
    adc edx, 0
mul2:
    shr ebx, 1
    shl eax, 1
    test ebx, ebx
    jnz  mul1
done:
    ret
mult endp

Другие советы

You do it in assembly the same way you do it on paper (long addition, long multiplication, long division). You split the number into parts, you carry, you borrow, and so on.

If you could only hold numbers between 0 and 9, you'd think of 32 as (10*3 + 2) or "32". To add, you add the lower part, carry if needed, then add the high part. To subtract, you change sign and add. To multiply, you use this:

(A*X + B) (C*X + D) = AC*X^2 + (AD+BC)*X + BD

It's the same way you do "32 * 12" as (3*10 + 2)(1*10 + 2) = (3*1)*100 + (2*1+3*2)*10 + 2*2 = 384

To divide, you do long division the same way you learned to do it on paper.

(Remember all those people who said you had to know math to program. This is one of the many reasons. You have to break things down into very tiny parts to program computers in assembly.)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top