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 / sumn-stack-prompt.asm
diff --git a/samples-with-macros/sumn-stack-prompt.asm b/samples-with-macros/sumn-stack-prompt.asm
new file mode 100644 (file)
index 0000000..063af10
--- /dev/null
@@ -0,0 +1,197 @@
+.model      small\r
+;#macro-commands-start\r
+; finish the execution\r
+end_execution macro\r
+    int 20h\r
+endm                      \r
+\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
+; 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
+; 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
+\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
+\r
+read_num macro num\r
+       read_str tempStr,6\r
+       ;push offset tempStr\r
+       ;push offset num\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
+;read_num, print_num, etc are macros\r
+\r
+start:  \r
+    mov ax, data\r
+    mov ds, ax            \r
+    print_str prompt\r
+    read_num n\r
+    mov cx, n\r
+l1:                 \r
+    print_str prompt\r
+       read_num num\r
+       push num\r
+       loop l1\r
+       ; now sum up the top n from the stack\r
+    mov cx, n\r
+    mov ax, 0\r
+    mov dx, 0\r
+theloop:\r
+    pop ax               ; get next from stack\r
+    add dx, ax        ; array sum is in dx\r
+       loop theloop\r
+       ; result            \r
+       print_new_line\r
+       print_str resStr\r
+       print_num dx\r
+end1:\r
+       nop\r
+       end_execution\r
+\r
+\r
+.data\r
+       num dw 12\r
+       n dw 3\r
+       rez dw 0 \r
+       tempStr db "        "\r
+       prompt  db "number? $"\r
+       resStr  db "result $"\r
+\r
+.stack\r
+end start\r
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner