;**************************************************************************** ; ; Name : Ali Haydar Ozer ; Student No : 9702452 ; Course : CmpE 230 Systems Programming ; Instructor : Can Ozturan ; Due Date : January 4,2000 ; Program Description : This program gets numbers a,n and displays ; the n'th power of a. ; ;******************************************************************************/ code segment ; PROGRAM CODE STARTS HERE ; Get the base mov di,temp ; Move address of temporary area to di mov si,base ; Move address of base to si mov bp,inpmsg1 ; Move address of first input message to bp call printmsg ; Display the input message related to base call readnum ; Read the number from keyboard and store it on temp call newline ; Print CRLF mov ax,w[di] ; Move the number from temp mov [si],ax ; to base ; Get the exponent mov si,exp ; Move address of exponent to si mov bp,inpmsg2 ; Move address of second input message to bp call printmsg ; Display the input message related to base call readnum ; Read the number from keyboard and store it on temp call newline ; Print CRLF mov ax,w[di] ; Move the number from temp mov [si],ax ; to exp ; Fast power algorithm starts here mov di,base ; Move address of base to di mov ax,w[di] ; Move base to ax mov bx,w[si] ; Move exponent to bx exploop: ; Main loop of fast power starts here cmp bx,0h ; Check whether exponent is 0 je endloop ; If exponent is 0 then end main loop mov cx,bx ; Move bx to cx (Not to lose exponent) and cx,1h ; Get the right most bit of exponent jne multiplybase; jump if right most bit of exponent is 1 contloop: ; This label is for multiplybase shr bx,1h ; Shift exponent 1 bit to right mul ax ; Get the square of base (dx is not necessary ; because result cannot be bigger than 65535) jmp exploop ; Jump to exploop multiplybase: ; This subrutine multiplies the result with current ; value of base push ax ; ax is necessary in main loop so push it onto stack mov bp,result ; Move the address of result to bp mul w[bp] ; Multiply result with current base mov [bp],ax ; Move the result to memory pop ax ; Pop the value of ax from stack jmp contloop ; Continue tthe main loop endloop: ; Result is found. So display the result mov bp,resmsg ; Move address of result message to base pointer call printmsg ; Display the result message mov bp,result ; Move the address of result to bp call printnum ; Print the result call newline ; Print CRLF finish: ; End of program int 20h ; Call interrupt 20h (Exit to Dos) ; Procedures start from here readnum: ; Reads a decimal number from keyboard push di ; Push di & si to stack, not to push si ; lose their values in the procedure xor si,si ; Clear si mov bp,temp ; bp stores address of temporary memory area input: mov ah,01h ; Read a character from keyboard int 21h ; (Call interrupt 21h, fuction 01h) cmp al,0dh ; Finish reading je processinp ; when enter is pressed cmp al,"0" ; If character is not jb errorinp ; a digit then print an error mov cl,"9" ; message and exit add cl,1h ; cmp al,cl ; jnb errorinp ; sub al,"0" ; Get the decimal value of the each character mov [bp+si],al ; Then move it to temp inc si ; Increment si jmp input ; Jump to input processinp: mov bx,1h ; Move 1 to bx (Digit multiplier) mov cx,10d ; Move decimal 10 to cx (Multiplication const.) mov di,si ; Let di store free memory to store result mov w[bp+di],0h ; Clear result to 0 processloop: ; Backward process starts here cmp si,0h ; Check whether si is 0 je endinp ; If si is 0 then jump to endinp (Process finished) dec si ; Decrement si by 1 mov ah,0h ; Move zero to ah (To multiply a byte with a word) mov al,b[bp+si] ; Move the decimal value of digit to al mul bx ; Multiply the digit with digit multiplier add w[bp+di],ax ; Add the result of multiplication to result which ; stores the number we look for mov ax,bx ; Move digit multiplier to ax mul cx ; Multiply the digit multiplier by 10 mov bx,ax ; Move result to bx jmp processloop ; Jump to processloop to process the next digit errorinp: ; If a non-digit character is entered display an ; error message and exit call newline ; Print CRLF mov bp,errormsg ; Move the address of error message to bp call printmsg ; Display the error message call newline ; Print CRLF jmp finish ; Finish the program endinp: ; Processing the number has finished mov ax,w[bp+di] ; The number is calculated so move it to beginning mov [bp],ax ; address of temp pop si ; Pop the content of si from stack pop di ; Pop the content of di from stack ret ; Procedure readnum finished. Return printnum: ; Prints the word in [bp] push di ; Push di & si to stack, not to push si ; lose their values in the procedure mov si,10d ; Move decimal 10 to si mov ax,[bp] ; Move the number shown by bp to ax xor dx,dx ; Clear dx xor cx,cx ; Clear cx nonzero: div si ; Divide number by 10 add dl,"0" ; Calculate its ASCII value push dx ; Push this value to stack xor dx,dx ; Clear dx inc cx ; Increment cx by 1 cmp ax,0h ; Check whether the quotient is 0 jne nonzero ; Continue the loop if the quotient is no-zero writeloop: ; Display the number pop dx ; Pop the ASCII value of digit from stack mov ah,02h ; Print it to screen int 21h ; (Call interrupt 21h, function 02h) dec cx ; Decrement cx by 1 jnz writeloop ; If there is more digit in stack continue printing pop si ; Pop the content of si from stack pop di ; Pop the content of di from stack ret ; Procedure printnum finished. Return printmsg: ; Prints the message in [bp] push di ; Push di & si to stack, not to push si ; lose their values in the procedure mov ah,02h ; Move 02h to ah xor si,si ; Clear si msgloop: mov dl,[bp+si] ; Move the character in [bp+si] to dl cmp dl,0h ; Check whether dl is 0 je endmsg ; If dl is zero then message ended. Jump to endmsg int 21h ; Call interrupt 21h, function 02h inc si ; Increment si by 1 jmp msgloop ; Jump to msgloop endmsg: pop si ; Pop the content of si from stack pop di ; Popthe content of di from stack ret ; Procedure printmsg finished. Return newline: ; Prints CRLF mov ah,02h ; Move 02h to ah mov dl,0ah ; Move ASCII value of carriage return to dl int 21h ; Print CR (Call interrupt 21h, function 02h) mov dl,0dh ; Move ASCII value of line feed to dl int 21h ; Print LF (Call interrupt 21h, function 02h) ret ; Procedure newline finished. Return inpmsg1: ; Message for getting base db 'Please enter base in decimal: ' db 0h inpmsg2: ; Message for getting exponent db 'Please enter exponent in decimal: ' db 0h resmsg: ; Message for displaying result db 'Result: ' db 0h errormsg: ; Message for error db 'Error: Only digits (0-9) are allowed!' db 0h base: ; Reserve 2 bytes for base dw 0h exp: ; Reserve 2 bytes for exponent dw 0h result: ; Reserve 2 bytes for result dw 01h ; Initialize result to 1 (base to the power 0 is 1) temp: ; Reserve the rest of segment to temp db ? code ends ; PROGRAM CODE ENDS HERE