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)
- Written fully in assembly, no external library for input/output, just interruptions.
- 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.
- Multiplication using repeated addition operations instead of the native “mult”.
- Divide by repeated subtraction and counting instead of the native “div”.
- Convert an ASCII String to Binary Number and vice versa. this is required to handle user input, and to display results.
- Basic Input/Output functions, like GetCh, PutCh, NewLine, WriteLine
- Custom Input/Output functions to read and write 128bit numbers, like Read128BitNum, Write128BitNum
- Basic Menu in Text Mode.
- 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.
And for large numbers (123456789 + 987654321)
Some of the functions used for this project.
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
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
mov dx, Msg1 ; call Write ; SEGMENT data ; Segment containing initialized data Msg1 DB "(1) ADD -> RESULT = A + B $"
; 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,'$'
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.