Processing Positive Integers in Linux
Solution
section .data
title DB "This program will add two positive numbers which are less than 100.", 0x0a
lenTitle EQU $ - title
prompt1 DB "Enter first number: "
lenPrompt1 EQU $ - prompt1
prompt2 DB "Enter second number: "
lenPrompt2 EQU $ - prompt2
resultMsg DB "Addition result: "
lenResultMsg EQU $ - resultMsg
LF DB 0xa
num1 DW 0; a place to save conversion of the first number
num2 DW 0; the place to save conversion of the second number
digit DB 0; a place to save digit of the result
section .bss
string1 RESB 3; a place to save a string of first number (2 digits + LF)
string2 RESB 3; a place to save a string of second number (2 digits + LF)
section .text
global _start
_start:
; Prompt user for the first number
mov eax, 4
mov ebx, 1
mov ecx, prompt1
mov edx, lenPrompt1
int 80h
;Read and store the user input, only reads at most 2 characters
mov eax, 3
mov ebx, 0
mov ecx, string1 ; save in string1
mov edx, 3 ; read at most 3 chars
int 80h
; Prompt user for the second number
mov eax, 4
mov ebx, 1
mov ecx, prompt2
mov edx, lenPrompt2
int 80h
;Read and store the user input, only reads at most 2 characters
mov eax, 3
mov ebx, 0
mov ecx, string2 ; save in string2
mov edx, 3 ; read at most 3 chars
int 80h
; convert ASCII string of the first number to an integer
mov esi, string1; point to start of string1 with si
mov bx, 0; bx will hold the converted number, start in zero
conv1:
xor cx, cx ; set cx to zero
mov cl, [esi] ; load a character in cl
CMP cl, 0xa; see if we reached the end of the string (LF char)
JE endConv1; if we reached the end, exit loop
sub cl, '0'; subtract 0 from the character to convert it to an integer
mov ax, 10; load 10 in ax for making a multiplication
mul BX; multiply the old number in bx by 10
mov bx, ax; save multiplication result in bx
add bx, cx ; add the digit we converted to the number
inc esi; advance to next digit character in the string
jump conv1; repeat to convert next digits
endConv1: at this point, bx has the value of the first number
mov [num1], bx ; save first number in variable
; convert ASCII string of the second number to an integer
mov esi, string2; point to start of string2 with esi
mov bx, 0; bx will hold a converted number, start in zero
conv2:
xor cx, cx ; set cx to zero
mov cl, [esi] ; load a character in cl
CMP cl, 0xa; see if we reached the end of the string (LF char)
je endConv2; if we reached the end, exit loop
sub cl, '0'; subtract 0 from the character to convert it to an integer
mov ax, 10; load 10 in ax for making a multiplication
mul BX; multiply the old number in bx by 10
mov bx, ax; save multiplication result in bx
add bx, cx ; add the digit we converted to the number
inc esi; advance to next digit character in the string
jmp conv2; repeat to convert next digits
endConv2: at this point, bx has the value of the second number
mov [num2], bx ; save second number in variable
; make the addition with the converted numbers
mov ax, [num1] ; load first number
add ax, [num2]; add to the second number and save the result in ax
; convert addition result to an ASCII digit string
mov bx, 10 ; load 10 in cx for making division
push bx; save a 10 in the stack
conv3:
mov dx, 0 ; clear dx to make division
div bx; divide the old number in ax by 10
push dx; save digit in the stack
CMP ax, 0; if the division of a number by 10 is zero, end loop
jne conv3; otherwise keep dividing it by 10
; print the result message
mov eax, 4
mov ebx, 1
mov ecx, resultMsg
mov edx, lenResultMsg
int 80h
; print converted result
print:
pop ax; load a digit from the stack
cmp ax, 10; if we found the 10 we have popped all digits, end loop
je exit
add al, '0'; else, convert digit to ASCII
mov [digit], al; save the digit in the variable for printing it
; print the digit
mov eax, 4
mov ebx, 1
mov ecx, digit
mov edx, 1
int 80h
jmp print; repeat to print all digits
exit:
; print a line feed
mov eax, 4
mov ebx, 1
mov ecx, LF
mov edx, 1
int 80h
; exit the program
mov eax, 1
xor ebx, ebx
int 80h