Last active
November 13, 2017 00:49
-
-
Save epk/064e74c58c207336930fc9b8ab67417d to your computer and use it in GitHub Desktop.
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
;------------------------------------------------------------------------------------------------------------------------------------------------------- | |
data segment ; data segment. Keyword db means define byte. You can also define word (dw) | |
iNum dw 2 dup(?) ;Reserve a byte for the decoded user input ;Define input number | |
tStr db 'Enter a perfect (unsigned 16-bit) square:',10,'$' ;Prompt for user | |
oStr db 'The result is:',10,'$' ;Output prompt | |
iBuff db 16, 17 dup(?) ;User input string ;Define input buffer | |
ten db 10 ;For readability | |
data ends | |
; stack segment | |
stack1 segment stack | |
db 100 dup(?) ; This is the stack of 100 bytes | |
stack1 ends | |
code segment | |
assume cs:code, ds:data, ss:stack1 | |
start: | |
;Perform initialization | |
mov ax, data ;Put the starting address of the data segment into the ax register (must do this first) | |
mov ds, ax ;Put the starting address of the data segment into the ds register (where it belongs) | |
mov ax, stack1 ;Put the starting address of the stack into the ax register (must do this first) | |
mov ss, ax ;Put the starting address of the stack segment into the ss register (where it belongs) | |
;------------------------------------------------------------------------------------------------------------------------------------------------------- | |
;****************** Perform Newton's Algorithm ****************** | |
call get_int ;Call procedure to get user input | |
;bl stores x_k | |
;al stores x_{k+1} | |
mov dx, iNum ;dl contains input number | |
mov bx, 1 ;x_0 = 1 | |
mov ah, 0 ;Clear ah | |
loop2S: | |
mov ax, dx ;Move input into al (for division) | |
div bx ;Divide by x_k | |
mov ax, 0 ;Clear ah (for addition) | |
add ax, bx ;Add x_k | |
shr ax,1 ;Divide by 2 | |
cmp ax, bx ;Check to see if they are the same | |
je loop2E ;Exit loop if they are equal | |
mov bx, ax ;Set x_k = x_{k+1} | |
jmp loop2S ;Start again | |
loop2E: | |
call print_int ;Output result | |
;------------------------------------------------------------------------------------------------------------------------------------------------------- | |
mov ah, 4ch ;Set up code to specify return to dos | |
int 21h ;Interpt number 21 (Return control to dos prompt) | |
;-------------------------------------------------------------------------------------------------------------------------------------------------- | |
;procedure to read an integer | |
;Input None: | |
get_int proc | |
mov ah, 9 ;Specify print string DOS service routine | |
lea dx, tStr ;Load pointer to string | |
int 21h ;Call DOS service routine | |
mov ah, 0ah ;Prepare to call buffered keyboard input | |
lea dx, iBuff ;Load the address of the buffer into iBuff | |
int 21h ;Request service from OS | |
mov ah, 2 ;Specify print string DOS service routine | |
mov dl, 10 ;Load new line feed into dl | |
int 21h ;Call DOS service routine | |
lea si, iBuff ;Point to start of buffer | |
inc si ;Point to # of chars | |
mov ch, 0 ;Clear ch for loop | |
mov cl, [si] ;Get the number of characters read | |
add si, cx ;Point si to end of buffer | |
mov dx, 0 ;Clear dl to create number | |
mov bx, 1 ;Stores power of 10 | |
gL1S: ;get_int loop1 start | |
mov ax, [si] ;Get digit | |
sub ax, 30h ;Convert to number | |
mul bx ;Multiply by power of 10 | |
add dx, ax ;Add to total | |
mov ax, bx ;Initialize X10 multiplication | |
mul ten ;Multiply by 10 | |
mov bx, ax ;Store power of 10 back in bl | |
dec si ;Decrement si | |
loop gL1S ;Loop until cx == 0 | |
mov iNum, dx ;Store result in memory | |
ret | |
get_int endp | |
;-------------------------------------------------------------------------------------------------------------------------------------------------- | |
;procedure that prints out an integer passed | |
;Input (al): unsigned 8-bit number for output | |
print_int proc | |
mov ah, 9 ;Specify print string DOS service routine | |
lea dx, oStr ;Load pointer to string | |
int 21h ;Call DOS service routine | |
mov dx, '$' ;Set up end of string | |
push dx ;Put on stack to indicate finished | |
PLS1: | |
mov ah, 0 ;Clear ah for division | |
div ten ;Divide by 10 | |
mov dl, ah ;Store remainder in dl (putting in dl makes output easier below) | |
push dx ;Store digit for output | |
cmp al, 0 ;Check to see if we are done looping | |
jne PLS1 | |
mov ah, 2h ;Setup character output routine | |
PLS2: | |
pop dx ;Get last character | |
cmp dl, '$' ;Check to see if we are finished | |
je pDone ;Exit loop if equal to $ | |
add dl, 30h ;Convert integer to character | |
int 21h ;Call character output routine | |
jmp PLS2 ;Continue looping | |
pDone: | |
ret | |
print_int endp | |
;-------------------------------------------------------------------------------------------------------------------------------------------------- | |
code ends | |
end start | |
;------------------------------------------------------------------------------------------------------------------------------------------------------- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment