flat assembler
Message board for the users of flat assembler.

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

Joined: 17 Jun 2003
Posts: 489
Location: Norway
Tommy 24 Jun 2003, 11:03
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
24 Jun 2003, 11:03
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum