Last active
January 15, 2023 19:06
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;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