flat assembler
Message board for the users of flat assembler.

Index > DOS > i wrote a Sample code for Outputting integers

Author
Thread Post new topic Reply to topic
Tommy



Joined: 17 Jun 2003
Posts: 492
Location: Norway
Tommy
Hi everybody!

Here comes two procedures to print signed and unsigned integers to the screen.... It can also print numbers in other bases given by the base register (bx). The number to print is given by the accumulator (ax)...

Code:
org 100h

mov ax,-1253 ; Number: -1253
call putsint ; Print it! (signed integer; base: 10 (default))
call newline

mov ax,4C8Fh ; Number: 4C8Fh (hex value)
mov bx,16    ; Base:   16 (hexadecimal)
call putint  ; Print it! (unsigned integer)
call newline

mov ax,4360  ; Number: 4360
mov bx,10    ; Base:   10 (decimal)
call putint  ; Print it! (unsigned integer)

xor ax,ax    ; Function: Wait for key...
int 16h

mov ax,4C00h ; Exit to DOS
int 21h

; Procedures

; Go to next line
newline:
  push ax dx
  mov ah,02h
  mov dl,0Dh
  int 21h    ; Output CR to screen
  mov dl,0Ah
  int 21h    ; Output LF to screen
  pop dx ax
ret

; Print a signed integer to screen
; bx = base (default = 10), ax = number
putsint:
  push ax dx
  cmp bx,0
  jne .start    ; if bx <> 0 then ...let's go!
  mov bx,10     ; else bx = 10
  .start:
  cmp bx,10
  jne .printit  ; if it's not a decimal integer, we don't print any signs
  cmp ax,0
  jns .printit  ; if it's not negative, we don't print any signs either
  push ax
  mov ah,02h
  mov dl,"-"
  int 21h       ; output the "-"
  pop ax
  neg ax        ; do the number positive
  .printit:
  call putint   ; now we can print the number...
  pop dx ax
ret

; Print an unsigned integer to screen
; bx = base (default = 10), ax = number
putint:
  push ax bx cx dx
  cmp bx,0
  jne .start    ; the same as in putsint... (if bx = 0 then bx = 10)
  mov bx,10
  .start:
  xor cx,cx     ; cx = 0
  .new:
  xor dx,dx     ; dx = 0
  div bx        ; number / base
  push dx       ; push the remainder
  inc cx        ; increase the "digit-count"
  cmp ax,0      ; if the quotient still is not 0, do it once more
  jnz .new
  .loop:
  pop dx        ; pop the remainder
  add dl,30h    ; convert the number to a character
  cmp dl,"9"
  jng .ok       ; if the charater is greater than "9" then we have
  add dl,7      ; to add 7 to get A as 10, B as 11, and so on...
  .ok:
  mov ah,02h    ; output the character
  int 21h
  loop .loop
  pop dx cx bx ax
ret
    

Enjoy!!!

Regards,
Tommy
Post 24 Jun 2003, 11:03
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.