Last active
November 27, 2017 12:57
-
-
Save epk/f1c80ea2465845710c52f42235e8e436 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) | |
DIM1 equ 5 ; define array size | |
A db DIM1 dup(?) ; initialize array | |
outStr db 'Enter Degree of Polynomial: $' ; For output | |
newL db 0ah,'$' ; newline For output | |
outPtr db 'Enter coefficient of x^?: $' ; For output | |
n dw 0 ; Stores the degree of polynomial | |
lastin dw 0 ; Stores the last coefficient | |
byte1 equ 23 ; Number of bytes to replace '?' in outPtr | |
byte2 equ 3 ; Number of bytes to replace '?' in polyStr | |
myArray dw 6 dup(?) ; array storing coefficients | |
polyOut db 'The Polynomial entered is',0ah,'$' ; for output | |
polyStr db '*x^$' ; for output | |
polyFinal db 'The first derivative is',0ah,'$' ;for output | |
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) | |
;------------------------------------------------------------------------------------------------------------------------------------------------------- | |
;****************** Enter code here ****************** | |
call get_poly ;Get degree of polynomial from user | |
mov cx, n ; move degree of polynomial into cx for loop | |
inc cx ; increment cx to access all elements in the loop | |
mov bx, 0 ; use bx to increase memory index of array in the loop | |
cmp cx, 0 ;Check to see if cx is 0 | |
je fEnd ;Jump to end if cx == 0 | |
print_test: | |
lea si, outPtr ;load address of outPtr | |
mov [si+byte1], cx ;replace ? in outPtr with cx | |
dec byte ptr [si+byte1] ;adjust to real value of degree of polynomials | |
add byte ptr [si+byte1], 30h ;Convert to character | |
mov ah, 9h ;Print string routine | |
mov dx, si ;load into dx for Print | |
int 21h ;call DOS | |
lea dx, newL ;Load newline | |
int 21h ;call DOS | |
call get_int ;call get_int to take in a coefficient | |
add bx,2 ;increase memory address by 2 | |
dec cx ;decrease cx for loop | |
jz fEnd ;jump to fEnd if zero | |
jmp print_test ;else keep looping | |
fEnd: | |
mov cx, n ;move degree of polynomial into cx for loop | |
inc cx ;increment cx to access all elements in the loop | |
mov ah, 9h ;Print string routine | |
lea dx, polyOut ;load into dx for Print | |
int 21h ;call DOS | |
lea si, myArray ;Load address of myArray into si | |
lea bp, polyStr ;Load address of polyStr into bp | |
poly_out: | |
mov ah, 2h ;print char sub routine | |
mov dl, [si] ;load coefficient for printing | |
int 21h ;call DOS | |
mov ah, 9h ;print string sub routine | |
mov dx, bp ;load polyStr for print | |
int 21h ;call DOS | |
mov ah, 2h ;print char sub routine | |
mov dx, cx ;move the degree of poly for print | |
add dx, 30h ;Ascii offset | |
dec dx ;adjust to real value (loop is cx+1) | |
int 21h ;call DOS | |
mov dx, 43 ;+ symbol for printing | |
int 21h ;call DOS | |
add si, 2 ;move to the next coefficient | |
dec cx ;decrease cx for loop | |
cmp cx, 1 ;compare if cx is 1 | |
je end_test ;if cx=1 jump to end_test to print last coefficient without degree and + sign | |
jmp poly_out | |
end_test: | |
mov ah, 2h ;print char sub routine | |
mov dx, lastin ;load last coefficient into dx for printing | |
int 21h ;call dos | |
mov ah, 2h ; | |
mov dx, 0ah ;Print new Line | |
int 21h ; | |
mov ah, 9h | |
lea dx, polyFinal | |
int 21h | |
mov cx, n ; for loop | |
mov bx, cx ; for output | |
add bx, 30h ;Ascii offset | |
lea si, myArray ;Load address of myArray into si | |
loop_2S: | |
mov ah, 2h ;print char sub routine | |
mov dx, 40 ;Print '(' | |
int 21h ;call dos | |
mov dx, bx ;Print n of x^n | |
int 21h ;call dos | |
mov dx, 42 ;Print '*' | |
int 21h ;call dos | |
mov dx, [si] ;Print coefficient | |
int 21h ;call dos | |
mov dx, 41 ;Print '*' | |
int 21h ;call dos | |
mov ah, 9h | |
lea dx, polyStr ;Print '*x^' | |
int 21h ;call dos | |
mov ah, 2h | |
mov dx, cx | |
add dx, 30h | |
sub dx, 1 | |
int 21h | |
mov ah, 2h | |
mov dx, 43 ;print "+" | |
int 21h ;call dos | |
add si, 2 ;point to next num in array | |
dec bx ;dec bx | |
dec cx ;dec cx | |
cmp cx, 0 ; | |
je end_all ; if cx=0 jump end_all | |
jmp loop_2S ; else jump loop_2S | |
end_all: | |
mov ah, 2h | |
mov dx, 48 ;print "+" | |
int 21h ;call dos | |
;------------------------------------------------------------------------------------------------------------------------------------------------------- | |
mov ah, 4ch ;Set up code to specify return to dos | |
int 21h ;Interpt number 21 (Return control to dos prompt) | |
;procedure to read the degree of the polynomial | |
;Input : None | |
;output : Stored in n | |
get_poly proc | |
push ax ;Back up ax | |
mov ah, 9h ;print string sub routine | |
lea dx, outStr ; load outStr for print | |
int 21h ; call dos | |
mov ah, 1 ;Specify Keyboard input with echo routine | |
int 21h ;Request service from the OS | |
mov ah, 0 ;Clear ah to store in memory | |
sub al, 30h ;Convert al to number | |
mov n, ax ;Put ax into n | |
mov ah, 2 ;Specify character output | |
mov dl, 0ah ;Specify new line | |
int 21h ;call DOS | |
pop ax ;Restore ax | |
ret | |
get_poly endp | |
;procedure to read the coefficient of the polynomial | |
;Input : bx pointing to memory location of myArray | |
;output :Stored in myArray and the last input when this proc was called stored in lastin | |
get_int proc | |
push ax ;Back up ax | |
mov ah, 1 ;Specify Keyboard input with echo routine | |
int 21h ;Request service from the OS | |
mov ah, 0 ;Clear ah | |
sub al, 30h ;Convert al to number | |
mov [myArray+bx], ax ;Store input into array location | |
add byte ptr [myArray+bx], 30h ;Ascii offset | |
mov lastin, ax ;Store input into lastin | |
add lastin, 30h ;Ascii offset | |
mov ah, 2 ;Specify character output | |
mov dl, 0ah ;Specify new line | |
int 21h ;call Dos | |
pop ax ;Restore ax | |
ret | |
get_int endp | |
code ends | |
end start | |
;------------------------------------------------------------------------------------------------------------------------------------------------------- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment