flat assembler
Message board for the users of flat assembler.

 Index > DOS > How to convert program to work with 32bit numbers ?
Author
umen

Joined: 14 Aug 2005
Posts: 16
umen 26 Aug 2005, 20:41
Hello
i have this simple program that do square calculation but with 16bit numbers
how can i take this program and make it work with 32 bit numbers ? so i can calculat numbers like :
1000000 and so on ?
any help and source codes or links will halp me allot
here is my code :
Code:
```-----------------------------------------------------------------------------------
LF   EQU  0AH
CR   EQU  0DH

DSEG SEGMENT
DSEG ENDS
SSEG SEGMENT STACK
DW  100H DUP(?)
SSEG ENDS
CSEG SEGMENT
ASSUME  CS:CSEG,DS:DSEG,SS:SSEG
BEGIN:
; uses di, bx, cx, dx
;
; square root of AX via Newton-Raphson iteration.
mov di,ax
mov ax,13
start_loop:
mov bx,ax
xor dx,dx
mov ax,di
div bx
shr ax,1
mov cx,ax
sub cx,bx
cmp cx,2
je  start_loop

CALL PRINT_AX_2

INT    21H
RET

PRINTCHAR:MOV  AH,2
INT  21H
RET

PRINT_NEWLINE: MOV  DL,CR
CALL  PRINTCHAR
MOV   DL,LF
CALL  PRINTCHAR
RET
;***************************************************************
print_ax_1      proc near

MOV     CL,4
MOV     DL,AL
SHR     DL,CL
CALL    PRINT_AX
MOV     DL,AL

AND     DL,0FH
CALL    PRINT_AX
MOV     DL,AH
SHR     DL,CL
CALL    PRINT_AX
MOV     DL,AH

AND     DL,0FH
CALL    PRINT_AX
ret
print_ax_1      endp

print_ax_2     proc near
MOV     CL,4
MOV     DL,AH
SHR     DL,CL
CALL    PRINT_AX
MOV     DL,AH
AND     DL,0FH
CALL    PRINT_AX
MOV     DL,AL
SHR     DL,CL
CALL    PRINT_AX
MOV     DL,AL
AND     DL,0FH
CALL    PRINT_AX
ret
print_ax_2      endp

PRINT_AX      PROC    NEAR
PUSH    AX
CMP     DL,'9'
JBE     OK
OK:           MOV     AH,2
INT     21H
POP     AX
RET
PRINT_AX        ENDP

CSEG ENDS
END BEGIN
-----------------------------------------------------------------------------------
```

Thanks allot!!!
26 Aug 2005, 20:41
Matrix

Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 26 Aug 2005, 22:35
hi,
first, change that je to jae then change registers to appropriate 32 bit ones,

Code:
```format MZ
entry codeseg:main

segment dataseg

segment codeseg

LF EQU 0AH
CR EQU 0DH

main:
mov ax,49

; uses di, bx, cx, dx
;
; square root of AX via Newton-Raphson iteration.
mov di,ax
mov ax,13
start_loop:
mov bx,ax
xor dx,dx
mov ax,di
div bx
shr ax,1
mov cx,ax
sub cx,bx
cmp cx,2
jae start_loop

CALL print_ax_2

INT 21H
RET ; exit

PRINTCHAR:MOV AH,2
INT 21H
RET

PRINT_NEWLINE: MOV DL,CR
CALL PRINTCHAR
MOV DL,LF
CALL PRINTCHAR
RET
;***************************************************************
print_ax_1: ;proc near

MOV CL,4
MOV DL,AL
SHR DL,CL
CALL PRINT_AX
MOV DL,AL

AND DL,0FH
CALL PRINT_AX
MOV DL,AH
SHR DL,CL
CALL PRINT_AX
MOV DL,AH

AND DL,0FH
CALL PRINT_AX
ret
;print_ax_1 endp

print_ax_2: ; proc near
MOV CL,4
MOV DL,AH
SHR DL,CL
CALL PRINT_AX
MOV DL,AH
AND DL,0FH
CALL PRINT_AX
MOV DL,AL
SHR DL,CL
CALL PRINT_AX
MOV DL,AL
AND DL,0FH
CALL PRINT_AX
ret
;print_ax_2 endp

PRINT_AX: ; PROC NEAR
PUSH AX
CMP DL,'9'
JBE OK
OK: MOV AH,2
INT 21H
POP AX
RET
;PRINT_AX ENDP
```

it will be the same basically, use eax instead of ax, ebx instead of bx,
26 Aug 2005, 22:35
umen

Joined: 14 Aug 2005
Posts: 16
umen 27 Aug 2005, 10:58
Hello and thanks for your replay
but my masm dont support using eax , edi and so on...
basically i like the program to except big numbers and perform
on them the Newton square root .
so as u see i can't implant your suggestion
27 Aug 2005, 10:58
El Tangas

Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas 27 Aug 2005, 17:42
If you cant use 32 bit registers, you'll have to use dx:ax to contain the 32 bit number, and make some changes to the calculations:

Code:
```1.
xor dx,dx
mov ax,di
div bx

you have to remove the xor dx,dx, of course

2.

you have to extend this to 32 bits:

3.
shr ax,1

extend to 32 bits:

shr dx,1
rcr ax,1

4.
In the final comparison, you have to check also that dx is zero.
```

And you may have to change other things to, but this is the most important.
27 Aug 2005, 17:42
umen

Joined: 14 Aug 2005
Posts: 16
umen 27 Aug 2005, 20:52
Hi
first of all tnx for the replay ... well i did changed my code as you suggested and i got
Divide overflow -- when i tried to run the program .. whets going on here ? can some one help me here ?
here is your changed i did in the code
-------------------------------------------------------------------------------
LF EQU 0AH
CR EQU 0DH

DSEG SEGMENT
DSEG ENDS
SSEG SEGMENT STACK
DW 100H DUP(?)
SSEG ENDS
CSEG SEGMENT
ASSUME CS:CSEG,DS:DSEG,SS:SSEG
BEGIN:
; uses di, bx, cx, dx
;
; square root of AX via Newton-Raphson iteration.

mov di,ax
mov ax,133
start_loop:
mov bx,ax
;xor dx,dx
mov ax,di
div bx
shr ax,1
rcr ax,1
mov cx,ax
sub cx,bx
cmp cx,2
jae start_loop

CALL PRINT_AX_2

INT 21H
RET

PRINTCHAR:MOV AH,2
INT 21H
RET

PRINT_NEWLINE: MOV DL,CR
CALL PRINTCHAR
MOV DL,LF
CALL PRINTCHAR
RET
;***************************************************************
print_ax_1 proc near

MOV CL,4
MOV DL,AL
SHR DL,CL
CALL PRINT_AX
MOV DL,AL

AND DL,0FH
CALL PRINT_AX
MOV DL,AH
SHR DL,CL
CALL PRINT_AX
MOV DL,AH

AND DL,0FH
CALL PRINT_AX
ret
print_ax_1 endp

print_ax_2 proc near
MOV CL,4
MOV DL,AH
SHR DL,CL
CALL PRINT_AX
MOV DL,AH
AND DL,0FH
CALL PRINT_AX
MOV DL,AL
SHR DL,CL
CALL PRINT_AX
MOV DL,AL
AND DL,0FH
CALL PRINT_AX
ret
print_ax_2 endp

PRINT_AX PROC NEAR
PUSH AX
CMP DL,'9'
JBE OK
OK: MOV AH,2
INT 21H
POP AX
RET
PRINT_AX ENDP

CSEG ENDS
END BEGIN
27 Aug 2005, 20:52
El Tangas

Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas 28 Aug 2005, 19:02
Well, you have to know what you're doing, that is, you have to understand the Newton algorithm when applied to the square root and the workings of the div instruction. For the program to work, you have to preserve the original number. In the original code:

mov di,ax

and restore it before the div:

mov ax,di
div bx

The div operation works like this: the 32 bit number in dx:ax is divided by the div operand (bx in this case). The result is placed in ax, and the remainder in dx, so both registers are changed.

You have to preserve and restore dx also:

preserve:
mov di,ax
mov si,dx

restore before the division:
mov dx,si
mov ax,di
div bx

maybe this will work.
28 Aug 2005, 19:02
umen

Joined: 14 Aug 2005
Posts: 16
umen 28 Aug 2005, 20:09
Hello ant tnx for the quick replay but i stiil get "Divide overflow" after the changes you gave me .
thanks
28 Aug 2005, 20:09
Matrix

Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 28 Aug 2005, 23:13
unfortunatly when the end product doesnt fit into 16 bits destiantion, ax, it gives this "Divide Overflow ..."

if you must use 16 bit assembler for 32 bit, then prefer using db s to extend to 32 bit

like in turbo pascal, because it generates 286 code only.

Code:
```db \$66
xor ax,ax ; this means xor eax,eax
db \$66
stosw
; stosd
db \$66
mov ax, \$faaa
db \$feee
; mov eax, \$feeefaaa
...
```

or simply complie with fasm and db it into your 286 assembler code.
if you just want to get square root of a number, you can use other codes, or simply use fpu.
28 Aug 2005, 23:13
umen

Joined: 14 Aug 2005
Posts: 16
umen 29 Aug 2005, 19:22
Hello and tnx again
confused again ..
cant i use 32 bit numbers ( big numbers ) with 2 registers ? in 16 masm ?
yes i like to get square root of number to understand and learn from it.
if i just could find working code example of such thing , im sure i could learn allot .
29 Aug 2005, 19:22
shoorick

Joined: 25 Feb 2005
Posts: 1608
Location: Ukraine
shoorick 30 Aug 2005, 08:18
of course, you can use some registers to work with long numbers, but you have to update higher parts depending on carry flag after each operation with lower part. but i cann't help you with division with this way (have no mathematical education )- i think, it is possible to find examples for this around i-net. a suggestion: if your pc is not 8086, place .386 (or .486 etc. - why hesitate?) at the start of source and use 32-bit registers and operations.
regards!
30 Aug 2005, 08:18
umen

Joined: 14 Aug 2005
Posts: 16
umen 30 Aug 2005, 19:21
hi , tnx for the replay what is "i-net" ?
and where i can find sources there?
30 Aug 2005, 19:21
UCM

Joined: 25 Feb 2005
Posts: 285
UCM 30 Aug 2005, 19:21
internet
30 Aug 2005, 19:21
shoorick

Joined: 25 Feb 2005
Posts: 1608
Location: Ukraine
shoorick 31 Aug 2005, 07:10
here is a program to play with double registers numbering. use 16-bit debugger to check how it works. (at least you have to have debug.exe in your system). you can change numbers to check and use calculator in hex mode to ensure if it works right.

_________________
UNICODE forever!
31 Aug 2005, 07:10
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum