Calculator in Assembly with NASM on Windows

Calculator in Assembly

this is a basic calculator written in assembly, it has some nice features, it was developed for the laboratory of  the Computer Architecture course of the Computer Science Engineering on the USAC (so it is an useful tool for learning purposes)

  1. Written fully in assembly, no external library for input/output, just interruptions.
  2. Internally use a 128 bit representation for holding operators and result. The requirement was operate (+,-) with numbers up to 20 digits, and (*,/) with numbers up to 10 digits.
  3. Multiplication using repeated addition operations instead of the native “mult”.
  4. Divide by repeated subtraction and counting instead of the native “div”.
  5. Convert an ASCII String to Binary Number and vice versa. this is required to handle user input, and to display results.
  6. Basic Input/Output functions, like GetCh, PutCh, NewLine, WriteLine
  7. Custom Input/Output functions to read and write 128bit numbers, like Read128BitNum, Write128BitNum
  8. Basic Menu in Text Mode.
  9. Easy to modify, build. The package include the assembler and linker so the executable can be created anytime. (using DOSBox if you are running on a modern operating system)

For Example, in this image sequence, we can see a simple addition.

assembler calculator main menu

Calculator Main Menu

assembler calculator addition result

Calculator after execution of a addition operation.

And for large numbers (123456789 + 987654321)

assembler calculator with large numbers

Calculator after execution of an addition of two large numbers.

Some of the functions used for this project.

GetCh

this function just read a key from the keyboard and puts it’s ascii code on the AL register.

; function GetCh
; get ascii code key pressed, BIOS int

GetCh:

xor ah,ah ; clear ah
int 0x16 ; call interruption
ret ; return

Write

this function takes the address of any string and display on screen, the string must end with a “$” character.

; function Write
; display a string on screen
;
; parameter dx string address
; the string must end with a '$' character

Write:

push ax            ; backup ax 
mov ah,0x9         ; func 9, display on screen
int 0x21           ; DOS interruption
pop ax             ; restore ax
ret                ; return

Usage Example

mov dx, Msg1  ; 
call Write    ;

SEGMENT data  ; Segment containing initialized data

Msg1   DB "(1) ADD -> RESULT = A + B $"

NewLine

; function NewLine
; new line , chr(13) + chr(10) on screen

NewLine:

mov dx, CRLF   ;
call Write     ;
ret            ;

SEGMENT data   ; Segment containing initialized data

CRLF   DB   0DH,0AH,'$'

WriteLn

this fuction just aggregate the standar Write with the predefined CRLF string, to make a string with newline.

; function Writeln
; string + newline
; parameter dx address of string to print

Writeln:

call Write          ; Display the string proper through Write
mov dx,CRLF         ; Load offset of newline string to DX
call Write          ; Display the newline string through Write
ret                 ; Return to the caller

This Video Explain how to Use, Modify and Rebuild the Source Code.

Download Source Code

by [googleplusauthor]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s