flat assembler
Message board for the users of flat assembler.

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

Author
Thread Post new topic Reply to topic
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 
    add ax,bx 
    shr ax,1 
    mov cx,ax 
    sub cx,bx 
    cmp cx,2 
    je  start_loop 
 
CALL PRINT_AX_2

 
 
READCHAR: MOV  AH,1
        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
      ADD     DL,30H
      CMP     DL,'9'
      JBE     OK
      ADD     DL,7
OK:           MOV     AH,2
      INT     21H
      POP     AX
      RET
PRINT_AX        ENDP

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



Thanks allot!!!
Post 26 Aug 2005, 20:41
View user's profile Send private message Reply with quote
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
add ax,bx
shr ax,1
mov cx,ax
sub cx,bx
cmp cx,2
jae start_loop

CALL print_ax_2

READCHAR: MOV AH,1
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
ADD DL,30H
CMP DL,'9'
JBE OK
ADD DL,7
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,
edi instead of di...
Post 26 Aug 2005, 22:35
View user's profile Send private message Visit poster's website Reply with quote
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
Post 27 Aug 2005, 10:58
View user's profile Send private message Reply with quote
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.
add ax,bx

you have to extend this to 32 bits:

add ax,bx
adc dx,0

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.
Post 27 Aug 2005, 17:42
View user's profile Send private message Reply with quote
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
add ax,bx
adc dx,0
shr ax,1
rcr ax,1
mov cx,ax
sub cx,bx
cmp cx,2
jae start_loop

CALL PRINT_AX_2



READCHAR: MOV AH,1
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
ADD DL,30H
CMP DL,'9'
JBE OK
ADD DL,7
OK: MOV AH,2
INT 21H
POP AX
RET
PRINT_AX ENDP

CSEG ENDS
END BEGIN
Post 27 Aug 2005, 20:52
View user's profile Send private message Reply with quote
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.
Post 28 Aug 2005, 19:02
View user's profile Send private message Reply with quote
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 .
im dispread here ...
can some one help please?
thanks
Post 28 Aug 2005, 20:09
View user's profile Send private message Reply with quote
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.
Post 28 Aug 2005, 23:13
View user's profile Send private message Visit poster's website Reply with quote
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 .
Post 29 Aug 2005, 19:22
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
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 Smile )- 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!
Post 30 Aug 2005, 08:18
View user's profile Send private message Visit poster's website Reply with quote
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?
Post 30 Aug 2005, 19:21
View user's profile Send private message Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM 30 Aug 2005, 19:21
internet
Post 30 Aug 2005, 19:21
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
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.


Description:
Download
Filename: umen2.1.zip
Filesize: 1.64 KB
Downloaded: 573 Time(s)


_________________
UNICODE forever!
Post 31 Aug 2005, 07:10
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.