Hi everybody,
I'm new in Assembly, and have made my first "good" application.
I want to have some feedback, optimizationtips, etc. so it'll me nice if you guys looked at it.
I didn't know where to post this on this forum, so I just posted it in MAIN. If I'm wrong, please move the thread, moderators.

It's written in 16-bit MASM, and looks like this...

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;      Author: v0id
;;      Date:  15-01-07
;;      Name:  Assembly Calculator (v0.1)
;;

%include macros.inc
.model small
.stack
.data
    welcomeMsg  db  10, 10, "v0id | Assembly Calculator v0.1", 10, 10, "  ", '$'
    errorMsg    db  10, 10, "An error occured...", 10, " ", "Only the operators '+' and '=' works!", '$'

.code
    _start:        mov  ax, @data                      ; Load DATA segment into AX
                    mov  ds, ax                        ; Move DATA segment from AX to DS
                    lea  dx, welcomeMsg                ; Load address of welcomeMsg into DX
                    mov  ah, 09h                        ; Move function 09h into AH (output string)
                    int  21h                            ; Use interrupt 21h (Dos Service)
                    mov  cx, 10                        ; Move 10 into CX
                    sub  dx, dx                        ; Clear DX
                    sub  bx, bx                        ; Clear BX
    _getCharacter:  mov  ah, 01h                        ; Move function 01h into AH (input character w/ echo)
                    int  21h                            ; Use interrupt 21h (Dos Service)
                    cmp  al, '0'                        ; Is AL equal '0'?
                    jl  _checkOperator                ; If less, jump to _checkOperator
                    cmp  al, '9'                        ; Is AL equal '9'?
                    jg  _checkOperator                ; If greater, jump to _checkOperator
                    jmp  _insert                        ; If none of above, jump to _insert
    _checkOperator: cmp  al, '+'                        ; Is AL equal '+'?
                    je  _plus                          ; If it's, jump to _instert
                    cmp  al, '='                        ; Is AL equal '='?
                    je  _finish                        ; If it's, jump to _finish
                    jmp  _error                        ; If none of above, jump to _error
    _plus:          add  bx, dx                        ; Add DX to BX
                    sub  dx, dx                        ; Clear DX
                    jmp  _getCharacter                  ; Jump back to _getCharacter
    _insert:        sub  al, '0'                        ; Convert from ASCII to decimal
                    cbw                                ; Convert bytes to word (AH and AL to AX)
                    push ax                            ; Push AX onto the stack
                    mov  ax, dx                        ; Move value of DX into AX
                    mul  cx                            ; Multiply AX by CX
                    mov  dx, ax                        ; Move result back into DX
                    pop  ax                            ; Restore AX from stack
                    add  dx, ax                        ; Add DX with AX
                    jmp  _getCharacter                  ; Jump back to _getCharacter
    _finish:        add  bx, dx                        ; Add DX to BX
                    puti bx                            ; Print the value of BX (final result)
                    jmp  _exit                          ; Exit the program
    _error:        lea  dx, errorMsg                  ; Load address of errorMsg into DX
                    mov  ah, 09h                        ; Move function 09h into AH (output string)                       
                    int  21h                            ; Use interrupt 21h (Dos Service)
    _exit:          mov  dl, 10                        ; Move a linebreak into DL
                    mov  ah, 02h                        ; Move function 02h into AH (output character)
                    int  21h                            ; Use interrupt 21h (Dos Service)
                    int  21h                            ; Use interrupt 21h (Dos Service)
                    mov  ax, 4C00h                      ; Move function 4Ch into AH (Terminate program)
                    int  21h                            ; Use interrupt 21h (Dos Service)
    end _start

It's a simple calculator, which works with the operators, plus and equal-sign. You type in some decimals (0..9) and can then use the operators (+, =) to addition and result. It's nothing big or good, but it's my first "real" program, and I wanted, as I said, have others to look at it.

The hardest part for me was to use the AL to multiply, because mul takes value from AX. So i searched at Google, and found cbw that works just fine!

In the _finish-label i use the macro puti which I've wrote, it takes a value and output it as a decimal to the screen. I didn't find it relevant to show the macros.inc, 'cus the only use of a homemade-macro in my program is puti. This also make me come with another small question; how exactly should I include files with MASM?

%include <filename>

This is what I did in my application, but if I look at the MASM directives, the only directive with the percent-sign (%) is %OUT? (Is it maybe because I look at directives in MASM 5.0, only resource I've at directives). My solution works fine, but is it more right to use something like;

include <filename>


Come with some feedback! :-)

(Sorry my english)
Posted on 2007-01-15 08:21:37 by v0id