Skip to content

Instantly share code, notes, and snippets.

@epk
Created November 13, 2017 01:15
Show Gist options
  • Save epk/3b25b39ca7297baf73f1f5119bb37bb2 to your computer and use it in GitHub Desktop.
Save epk/3b25b39ca7297baf73f1f5119bb37bb2 to your computer and use it in GitHub Desktop.
;-------------------------------------------------------------------------------------------------------------------------------------------------------
data segment ; data segment. Keyword db means define byte. You can also define word (dw)
iNum db 1 dup(?) ;Reserve a byte for the decoded user input ;Define input number
tStr db 'Enter a perfect (unsigned 8-bit) square:',10,'$' ;Prompt for user
oStr db 'The result is:',10,'$' ;Output prompt
iBuff db 4, 5 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 dl, iNum ;dl contains input number
mov bx, 1 ;x_0 = 1
mov ah, 0 ;Clear ah
loop2S:
mov al, dl ;Move input into al (for division)
div bl ;Divide by x_k
mov ah, 0 ;Clear ah (for addition)
add al, bl ;Add x_k
shr al,1 ;Divide by 2
cmp al, bl ;Check to see if they are the same
je loop2E ;Exit loop if they are equal
mov bl, al ;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 dl, 0 ;Clear al to create number
mov bl, 1 ;Stores power of 10
gL1S: ;get_int loop1 start
mov al, [si] ;Get digit
sub al, 30h ;Convert to number
mul bl ;Multiply by power of 10
add dl, al ;Add to total
mov al, bl ;Initialize X10 multiplication
mul ten ;Multiply by 10
mov bl, al ;Store power of 10 back in bl
dec si ;Decrement si
loop gL1S ;Loop until cx == 0
mov iNum, dl ;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