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-input.asm
diff --git a/samples-with-macros/rek-gcd-input.asm b/samples-with-macros/rek-gcd-input.asm
new file mode 100644 (file)
index 0000000..eb72549
--- /dev/null
@@ -0,0 +1,222 @@
+.model      small     \r
+ASSUME DS:data\r
+;recursive version of GCD, input from terminal\r
+data segment\r
+  strNZD db "        "\r
+  tempStr db "        "\r
+  prompt  db "num?$"\r
+  promptr db "RES $"\r
+  m     dw  0\r
+  n     dw  0\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,strNZD\r
+       print_str strNZD\r
+endm                   \r
+\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
+    push n\r
+    push m\r
+    call gcd\r
+    pop n    \r
+    print_str promptr\r
+       print_num n\r
+; print out a result\r
+    end_execution\r
+\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
+\r
+.stack\r
+end start\r
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner