Skip to content

Instantly share code, notes, and snippets.

@AggamR
Last active January 15, 2023 19:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AggamR/fe9af488818061de0b1c6d33ec921214 to your computer and use it in GitHub Desktop.
Save AggamR/fe9af488818061de0b1c6d33ec921214 to your computer and use it in GitHub Desktop.
sqrt in TASM MS-DOS assembly. doesn't print result - just keeps it in memory.
;Aggam Rahamim
IDEAL
MODEL small
STACK 100h
DATASEG
; --------------------------
; Your variables here
; --------------------------
smaller db 0
final db 0 ; final result
num db 9 ; number to sqrt
CODESEG
proc sqrt
xor cx, cx
mov cl, 1
sqrtLoop:
xor ax, ax
mov al, cl
mul cl
cmp al, [num]
ja sqrtAbove
je sqrtEqual
sqrtSmaller:
mov [smaller], cl
jmp contLoop
sqrtAbove:
xor ax, ax
mov al, cl
add al, [smaller]
mov bl, 2
div bl
mov [final], al
jmp endLoop
sqrtEqual:
mov [final], cl
jmp endLoop
contLoop:
inc cl
cmp cl, [num]
jb sqrtLoop
endLoop:
ret
endp
start:
mov ax, @data
mov ds, ax
; --------------------------
; Your code here
; --------------------------
mov [num], 27 ; number to sqrt
call sqrtLoop
exit:
mov ax, 4C00h
int 21h
END start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment