Multiplying arbitrarily long sequences of ASCII digits by a single-digit multiplier

For example my buffer contains: 456
I need that the output looked like 456*3 which equals to 1368

In contrast to your current approach that starts at the end of the source string, combining the digits into a 16-bit integer ready for multiplication, is best done beginning at the start of the sequence of digits:

    mov  cx, ax
    mov  si, offset skBuf
    mov  di, offset raBuf
    xor  ax, ax
    xor  bx, bx
  work:
    mov  dx, 10
    mul  dx          ; AX = 0 then AX = 40 then AX = 450
    mov  bl, [si]
    inc  si
    sub  bl, 48
    add  ax, bx      ; AX = 4 then AX = 45 then AX = 456
    loop work

    mov  dx, 3
    mul  dx          ; AX = 1368

Then use one of the code snippets from Displaying numbers with DOS
that you can freely modify to have it store into your destination buffer.

They expect me to do the same calculation to almost any sized number:/ that’s why I posted asking to do the exact thing you mentioned by doing it by hand i guess. Do you have any ideas how to work with that? It’s pretty much what I asked in the beginning but maybe my question was incorrectly worded

Then you need the long multiplication with a single-digit multiplier. This time it’s easiest to begin at the end of the digit sequence and also store the result from the end of the destination buffer, which should have room for 1 extra character!

  mov  bx, ax      ; Length source
  lea  si, skBuf[bx - 1]
  lea  di, raBuf[bx]
  xor  cl, cl      ; Clear our carry
work:
  mov  al, [si]    ; Read character
  dec  si
  sub  al, 48      ; AL is [0,9]
  mov  ah, 3
  mul  ah          ; (digit * 3) -> AX is [0,27]
  add  al, cl
  aam              ; (digit * 3) % 10 -> AL is [0,9]
                   ; (digit * 3) / 10 -> AH is [0,2]
  add  al, 48
  mov  [di], al    ; Write character
  dec  di
  mov  cl, ah      ; (digit * 3) / 10 -> CL is carry [0,2]
  dec  bx
  jnz  work

Leave a Comment