; Number Addition Challenge - DNRY ; ------------------------------------------------------ ; Ask the user to input two values between 0-9999 ; Accept two user inputs ; Add the two user inputs together into a memory address ; The program can end with the sum in the memory address ; Hint: remember that each digit is stored in ascii ; Updated for DNRY - use functions to limit repeating code ; ------------------------------------------------------ ; Data section value1: db 0x04 db [0x00,0x05] value2: db 0x04 db [0x00,0x05] string1: db "Please Enter Value 1" string2: db "Please Enter Value 2" null: db 0x0 sum: dw 0x0000 ; Functions section ; Function to print a string def print_message{ mov AH, 0x13 sub CX, BP int 0x10 ret } def convert_ascii_raw { mov CL, byte [BX,1] ; Put length of value 1 array into CX for looping mov SI, BX add SI, 0x02 convert_to_decimal_1: lods byte ; Load byte of ascii into AL sub AL, 0x30 ; Convert from ascii to raw number push CX ; Preserve CX for outerloop dec CX ; Counter needs to decrement to account for first number being in 0 position mov AH, 0x00 ; Zero out AH so it's empty for math mov DX, AX ; Put AX into BX so we can use AL to hold the smaller mul number of 10 cmp CX, 0x00 ; Check if CX is 0, if it we don't need to do exponent bc it's last digit je after_exp_1 ; If it is 0 jump over exponent exp_loop_1: mov AX, 10 ; Mov 10 into AL so we can multiply mul DX ; Multiply the contents of BX by 10 mov DX, AX ; The results of mul will be in AX, mov back to BX to mul again if needed loop exp_loop_1 ; Loop back and do exponent again if needed after_exp_1: pop CX ; Pop the initial CX back for the outer loop to count through remaining digits pop BX add word [BX], DX ; Add the result to the total sum push BX loop convert_to_decimal_1 ; Loop back for next digit if required pop AX ret } start: ; Print Message 1 mov CX, offset string2 mov BP, offset string1 call print_message ; Accept Value 1 mov AH, 0xa mov DX, offset value1 int 0x21 ; Print Message 2 mov CX, offset null mov BP, offset string2 call print_message ; Accept Value 2 mov AH, 0xa mov DX, offset value2 int 0x21 ; Convert Value 1 contents from ascii to decimal mov BL, offset value1 lea AX, word sum push AX call convert_ascii_raw ; Convert Value 2 contents from ascii to decimal, and add them to sum mov BL, offset value2 lea AX, word sum push AX call convert_ascii_raw