Finding Factorial of 8, clr screen and printing result with Assembly
Note: ; using 16bit assembly programming and NASM assembler.
[ORG 100H] ; instialization of .com file JMP MAIN ; jump to lable Main string: db "= Result of Factorial 8 ( by r_t )$" ; string to print Facto: ; subroutine to fine factorial of 8 MOV CX,7 ; MOV AX,8 L1: MUL CX ; multiplying ax and cx SUB CX, 1 ; decrimenting cx by 1 JNZ L1 ; geting loop until cx goes to 0 RET PrintNumber: ; subroutine to print integer number on screen push bp mov bp, sp push es push ax push bx ; pussing some registers on stack push cx push dx push di MOV AX, 0XB800 ; video base address in ax MOV ES, AX ; pointing es to video base MOV AX, [bp+4] ; loading number in ax MOV BX, 10 ; using base 10 for division to get decimal digits MOV CX, 0 ; intializing count of digits NextDigit: MOV DX, 0 ; 0 upper half of dividend DIV BX ; dividing by 10 ADD DL, 0x30 ; adding 30 in remainder to get ASCII values PUSH DX ; saving last got ASCII value on stack INC CX ; incrementing count of values CMP AX, 0 ; checking, is the qoutient reached to zero ? JNZ NextDigit ; if not, then do it again mov di, 0 ; pointing di to top left column NextPosition: ; its time to print on screen in correct order POP DX ; poping last stored ASCII value from the stack MOV DH, 0x07 ; adding normal attribut mov [es:di], dx ; printing character on screen add di, 2 ; moving to next screen location( 1 byte of attirbute + 1 byte of actual character) loop NextPosition ; do this for all the ASCII values stored on stack pop di pop dx pop cx pop bx ; restoring all registers pop ax pop es pop bp ret 2 ; returning from routine by clearing stack Clrscr: ; subroutine to clear the whole screen push es push ax push di mov ax, 0xb800 mov es, ax ; pointing es to video base mov di, 0 ; di is pointing top left column of screen nextloc: mov word [es:di], 0x0720 ; clearing next character on screen add di, 2 ; moving to next location of screen cmp di, 4000 ; checking, is whole screen cleared? jne nextloc ; if not then repeat pop di pop ax pop es ret GETCH: ; subroutine to get character from keyboar(acutally MOV AH, 0 INT 0X16 ; i am using it to stop the screen on last results) RET ; using keyboard services MAIN: ; our main body of the program starts from here CALL Facto ; calling subrouting of factorial PUSH AX ; saving last counted factorial's result on stack CALL Clrscr ; clearing whole screen CALL PrintNumber ; calling routine to print that result on screen ; setting cursor position using video services mov ah, 02h mov bh, 0 mov dh, 0h mov dl, 7h int 0x10 ;printing string using dos services mov ah, 9 mov dx, string int 0x21 CALL GETCH ; calling getch() funtion MOV AX, 0X4C00 ; terminating the program with no error INT 0X21
Output:
No comments: