gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
version 0.81
[asm2wsl.git] / samples-with-macros / rek-gcd-predef.asm
diff --git a/samples-with-macros/rek-gcd-predef.asm b/samples-with-macros/rek-gcd-predef.asm
new file mode 100644 (file)
index 0000000..84c2eba
--- /dev/null
@@ -0,0 +1,120 @@
+.model      small     \r
+;recursive version of GCD, predefined input\r
+data segment\r
+  strNZD db "        "\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
+\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,strNZD\r
+       print_str strNZD\r
+endm\r
+;#macro-commands-end\r
+\r
+.code\r
+    \r
+start:\r
+    push 8\r
+    push 12\r
+    call gcd\r
+    pop ax\r
+       print_num ax\r
+; print out a result\r
+    end_execution\r
+    \r
+;volatile procedure, ruins ax,bx,cx\r
+gcd proc\r
+;#extra-start\r
+    pop cx ;ret adress    \r
+;#extra-end\r
+    ;get params\r
+    pop ax\r
+    pop bx\r
+;#extra-start\r
+    push cx ;ret for later \r
+;#extra-end\r
+    cmp ax,bx\r
+    je endequal\r
+    ja greatera\r
+    ;ensure ax is greater\r
+    xchg ax,bx          \r
+greatera:\r
+    sub ax,bx\r
+    push bx\r
+    push ax\r
+    call gcd\r
+    pop ax;result\r
+    \r
+endequal:\r
+;#extra-start\r
+    pop cx\r
+;#extra-end\r
+    push ax; result\r
+;#extra-start\r
+    push cx;needed before ret\r
+;#extra-end\r
+    ret\r
+gcd endp\r
+.stack\r
+end start\r
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner