Stu's Rusty Bucket

Digging out an old assembly routine

This is an old assembly x86 routine for printing a number from 0-65535.
Translated from funky old a86 to Nasm syntax.

   1:;; call with number in ax, output buffer in di
   2:;; clobbers ax+di
   3:;; di points to end of string
   4:
   5:PrintNumber:
   6:    push bx
   7:    push cx
   8:    push dx
   9:    
  10:    mov bx,10
  11:    xor cx,cx
  12:    push cx
  13:    inc cx
  14:
  15:.l1:
  16:    xor dx,dx       ;; clear high order part of number (where x greater than  65535)
  17:    div bx
  18:    or dl,0x30      ;; add '0' to the remainder
  19:    push dx
  20:    inc cx          ;; increment digit count
  21:    or ax,ax        ;; loop until no numbers left
  22:    jnz .l1
  23:
  24:.l2:
  25:    pop ax          ;; drop it in the buffer
  26:    stosb
  27:    loop .l2
  28:
  29:    pop dx
  30:    pop cx
  31:    pop bx
  32:
  33:    ret
  34:


and if you dont care about clobbering registers when you call the routine, cut out the push/pops.

   1:;; call with number in ax, buffer in di
   2:;; clobbers ax+bx+cx+dx+di
   3:
   4:PrintNumber:
   5:    mov bx,10
   6:    xor cx,cx
   7:    push cx
   8:    inc cx
   9:
  10:.l1:
  11:    xor dx,dx       ;; clear high order part of number (where x greater than 65535)
  12:    div bx
  13:    or dl,0x30      ;; add '0' to the remainder
  14:    push dx
  15:    inc cx          ;; increment digit count
  16:    or ax,ax        ;; loop until no numbers left
  17:    jnz .l1
  18:
  19:.l2:
  20:    pop ax          ;; drop it in the buffer
  21:    stosb
  22:    loop .l2
  23:
  24:    ret
  25:


This wont deal with signed, it just prints a raw number. Printing a number over 65535 reuquires a slight modification but nothing major (an xchg ax,dx and an extra division)..
Posted by on 08/27 at 08:14 AM    
Filed Under : ComputersDevelopment
Comments are closed There are no comments on this entry.

Next entry: Hosing Open2x

Previous entry: My New shotgun

<< Back to main