Jaken_1 0 Newbie Poster

Like the title says. I have a user-inputted string that I need to do math calculations with, therefore, I need to convert the string to an integer. This is my current code. What can I do to manipulate the number the user inputs?

org 0x100

section .data
   prompt   db   "Enter a Base 10 Number",13,10,'$'
   len   equ $ -prompt
   inbinary db   "In binary it is ",13,10,'$'
   inoctal  db   "In octal it is ",13,10,'$'
   inhex    db   "In hexadecimal it is ",13,10,'$'


section .text
start:
   push   cs
   pop    ax
   mov    ds, ax
   mov    es, ax      ; make sure ds = es = cs

   mov    di, string  ; es:di points to string
   cld                ; clear direction flag (so stosb incremements rather than decrements)


   mov    ah, 9
   mov    dx, prompt
   int    21h

read_loop:
   mov    ah, 0x01    ; Function 01h Read character from stdin with echo
   int    0x21
   cmp    al, 0x0D    ; character is carriage return?
   je     read_done   ; yes? exit the loop
   stosb              ; store the character at es:di and increment di
   jmp    read_loop   ; loop again
read_done:
   mov    al, '$'
   stosb              ; 'Make sure the string is '$' terminated


   mov    bx, string  ; store string in base register

   mov    dx, bx      ; ds:dx points to bx
   mov    ah, 9       ; Function 09h Print character string
   int    0x21

   ; Exit
   mov    ax, 0x4c00
   int    0x21

   string:
   times    255 db 0  ; reserve room for 255 characters

Dani AI

Generated

posted a program that reads typed characters into a buffer and echoes them back. To perform arithmetic on that input it must be converted from ASCII digits to a binary integer. The standard approach is simple and robust: skip optional whitespace/sign, loop over digit characters, subtract '0' to get the numeric value, multiply the running result by 10 and add the digit. Detect non-digit terminators (CR, NUL, or the buffer's terminator) and check for 16-bit overflow before the multiply/add.

A compact 16-bit routine that implements this (unsigned decimal, DS:SI -> string, stops on CR or NUL) is shown below. On success AX contains the value; on overflow the routine returns AX = 0xFFFF as an error indicator. Registers clobbered: AX,BX,CX,DX,CL.

; str_to_u16: convert ASCII decimal at DS:SI -> AX (unsigned)
; stops at CR (0x0D) or NUL; on overflow returns AX=0xFFFF
str_to_u16:
    xor ax, ax        ; result = 0
    xor bx, bx
    xor cx, cx
.next_char:
    mov cl, [si]
    cmp cl, 0x0D
    je .done
    cmp cl, 0
    je .done
    cmp cl, '0'
    jb .done
    cmp cl, '9'
    ja .done
    sub cl, '0'       ; cl = digit (0..9)
    mov dx, ax
    cmp dx, 6553      ; 65535 / 10 = 6553.5 -> floor 6553
    ja .overflow
    je .check_last
    mov bx, 10
    mul bx            ; AX = AX * 10
    add al, cl
    adc ah, 0
    inc si
    jmp .next_char
.check_last:
    cmp cl, 5
    ja .overflow
    mov bx, 10
    mul bx
    add al, cl
    adc ah, 0
    inc si
    jmp .next_char
.done:
    ret
.overflow:
    mov ax, 0FFFFh
    ret

Practical notes: the posted program uses a '$' terminator; adapt the terminator test if required. For safer, user-friendly input consider DOS buffered input (int 21h AH=0Ah) which handles backspace and returns the entered length. For negative numbers, detect a leading '-' and neg ax after conversion. For math beyond 16 bits use wider accumulation or detect overflow and switch to 32-bit arithmetic (DX:AX or a multiword routine). Once the value is in AX, standard instructions (add, sub, mul/imul producing DX:AX) can perform arithmetic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.