gitweb on Svarog
projekti pod git sistemom za održavanje verzija -- projects under the git version control systemdiff --git a/samples-with-macros/gcd-input.asm b/samples-with-macros/gcd-input.asm
--- /dev/null
@@ -0,0 +1,198 @@
+.model small\r
+;greatest common divisor, with input from terminal\r
+data segment\r
+ tempStr db " "\r
+ prompt db "num?$"\r
+ promptr db "RES $"\r
+ m dw 0\r
+ n dw 0\r
+\r
+data ends\r
+;#macro-commands-start\r
+; print a string on screen\r
+print_str macro s\r
+ push ax\r
+ push dx \r
+ mov dx, offset s\r
+ mov ah, 09\r
+ int 21h\r
+ pop dx\r
+ pop ax\r
+endm\r
+\r
+; new line\r
+print_new_line macro\r
+ push ax\r
+ push bx\r
+ push dx\r
+ mov ah,03\r
+ mov bh,0\r
+ int 10h\r
+ inc dh\r
+ mov dl,0\r
+ mov ah,02\r
+ int 10h\r
+ pop dx\r
+ pop bx\r
+ pop ax\r
+endm\r
+\r
+; write a single char\r
+print_char macro c\r
+ push ax \r
+ push dx\r
+ mov ah, 02\r
+ mov dl, c\r
+ int 21h\r
+ pop dx\r
+ pop ax\r
+endm\r
+\r
+; finish the execution\r
+end_execution macro\r
+ mov ax, 4c02h\r
+ int 21h\r
+endm \r
+\r
+; Konvertovanje broja u string\r
+inttostr macro num1 str1\r
+ push ax\r
+ push bx\r
+ push cx\r
+ push dx\r
+ push si\r
+ mov ax, num1\r
+ mov dl, '$'\r
+ push dx\r
+ mov si, 10\r
+petlja2:\r
+ mov dx, 0\r
+ div si\r
+ add dx, 48\r
+ push dx\r
+ cmp ax, 0\r
+ jne petlja2\r
+ \r
+ mov bx, offset str1\r
+petlja2a: \r
+ pop dx\r
+ mov [bx], dl\r
+ inc bx\r
+ cmp dl, '$'\r
+ jne petlja2a\r
+ pop si \r
+ pop dx\r
+ pop cx\r
+ pop bx\r
+ pop ax \r
+endm\r
+\r
+print_num macro num\r
+ inttostr num,tempStr\r
+ print_str tempStr\r
+endm\r
+read_num macro num\r
+ read_str tempStr,6\r
+ strtoint tempStr,num\r
+endm\r
+\r
+;read into a buffer, inc the special 0 and 1 bytes\r
+;fixes it into a $ terminated string\r
+read_str macro strbuff, strlen\r
+ LOCAL copystr\r
+ push ax\r
+ push bx\r
+ push cx\r
+ push dx\r
+ push si\r
+ mov bp, sp\r
+ mov dx, offset strbuff\r
+ mov bx, dx\r
+ mov ax, strlen\r
+ mov byte [bx] ,al\r
+ mov ah, 0Ah\r
+ int 21h\r
+ mov si, dx \r
+ mov cl, [si+1] \r
+ mov ch, 0\r
+copystr:\r
+ mov al, [si+2]\r
+ mov [si], al\r
+ inc si\r
+ loop copystr \r
+ mov [si], '$'\r
+ pop si \r
+ pop dx\r
+ pop cx\r
+ pop bx\r
+ pop ax\r
+endm\r
+\r
+;ascii to actual number\r
+strtoint macro inStr,outNum\r
+ LOCAL mainloop,end\r
+ push ax\r
+ push bx\r
+ push cx\r
+ push dx\r
+ push si\r
+ mov bp, sp\r
+ mov bx, offset inStr\r
+ mov ax, 0\r
+ mov cx, 0\r
+ mov si, 10\r
+mainloop:\r
+ mov cl, [bx]\r
+ cmp cl, '$'\r
+ je end\r
+ mul si\r
+ sub cx, 48\r
+ add ax, cx\r
+ inc bx \r
+ jmp mainloop\r
+end:\r
+ mov outNum, ax \r
+ pop si \r
+ pop dx\r
+ pop cx\r
+ pop bx\r
+ pop ax\r
+endm\r
+;#macro-commands-end\r
+\r
+.code \r
+\r
+start: \r
+ mov ax,@data\r
+ mov ds,ax\r
+ print_str prompt\r
+ read_num m \r
+ print_new_line\r
+ print_str prompt\r
+ read_num n\r
+ print_new_line\r
+\r
+ mov ax,n\r
+ mov bx,m\r
+\r
+compare: \r
+ cmp ax,bx\r
+ je exit ;exit since they're equal\r
+ ja greater\r
+ sub bx,ax\r
+ jmp compare\r
+\r
+greater:\r
+ sub ax,bx\r
+ jmp compare\r
+\r
+exit:\r
+;exit out of the program\r
+ mov n,ax\r
+ print_str promptr\r
+ print_num n\r
+; print out a result\r
+ end_execution\r
+\r
+.stack\r
+end start\r