atoi(), itoa() - conversion routines

All 680x0 related coding posts in this section please.

Moderators: simonsunnyboy, Mug UK, Zorro 2, Moderator Team

tcat
Obsessive compulsive Atari behavior
Obsessive compulsive Atari behavior
Posts: 110
Joined: Fri May 03, 2013 6:00 am

atoi(), itoa() - conversion routines

Postby tcat » Thu Jan 25, 2018 7:38 pm

Hi,

I have always taken for granted, standard input/output routines in higher level languages such as `BASIC' ,`C' ,`Pascal', etc . I was therefore wondering how to code simple conversion routines in assembly language.

This sets a basis of my assembly learning excercise, I reused a Simon's template in effort to adopt some good ST coding practise. Sharing both code and prog.

convert.png

Code: Select all

   ; ---------------------------
   ; ATARI CONVERSION ROUTINES
   ; ---------------------------
   ; Version 25 Jan 2018 [tcat]
   ; atoi(),itoa(),signed,unsigned
   ;

gemdos equ 1
xbios equ 14
bios equ 13

   text

; init program heap and stack
init:   
   move.l sp,a5
   lea stack,sp       ; set user stack

; calculate program size - free unused space
   movea.l 4(a5),a5   ; a5 points to base page
   move.l $c(a5),d0   ; size of TEXT
   add.l $14(a5),d0   ; size of DATA
   add.l $1c(a5),d0   ; size of BSS
   add.l #$100,d0     ; size of base page
   move.l d0,-(sp)    ; total
   move.l a5,-(sp)   
   move.w #0,-(sp)
   move.w #$4a,-(sp)
   trap #gemdos       ; Mshrink, shrink memory
   lea 12(sp),sp      ; correct stack
   tst.l d0
   bne exit           ; exit if error
   

; -------------------------
; main program entry point:
; -------------------------
main:

; set palette
;
   pea my_palette
   move.w #$26,-(sp)   ; Supexec, call in supervisor
   trap #xbios
   addq.l #6,sp


; print banner
;
   pea banner
   move.w #9,-(sp)   ; Cconws, write string to console
   trap #gemdos
   addq.l #6,sp


; wait for user input
;
wait_input:
   pea .prompt      ; input prompt
   move.w #9,-(sp)   ; Cconws, write string to console
   trap #gemdos
   
   lea .input+2,a0   ; input buffer starts here
   clr.l (a0)+      ; clear buffer
   clr.l (a0)
   pea .input      ; read user input
   move.w #10,-(sp)   ; Cconrs, read string from console
   trap #gemdos

   pea .crlf      ; print new line
   move.w #9,-(sp)   ; Cconws, write string to console
   trap #gemdos

   ; optional
*  pea .input+2      ; echo user input
*  move.w #9,-(sp)   ; Cconws, write string to console
*  trap #gemdos

   lea 18(sp),sp   ; tidy stack

   lea .input+2,a0   ; input alpha string ->a0
   bsr.w atoi      ; convert to integer ->d0
   move.l d0,d5      ; save result
   bsr.w print_hex   ; print hexadecimal
   move.l d5,d0      ; restore result
   bsr.w print_dec   ; print decimal
   
   ; another input
   bra.w wait_input

.prompt: dc.b 13,10,"Input? ",0
.input:  dc.b 6,0
         dcb.b 8,0
.crlf:   dc.b 13,10,0


; terminate program properly and return to desktop or shell...   
exit:   clr.l -(sp)
   trap #gemdos   ; Pterm0, terminate process


;------------------------
; SUBROUTINES/ FUNCTIONS
;------------------------

; atoi(), alpha to integer (signed)
; conversion  routine, a0 points to string
;
atoi:
   clr.l d0
   clr.l d1
   clr.b .minus
   cmpi.b #'-',(a0)   ; look for minus sign
   bne.w .loop
   addq.l #1,a0
   tas.b .minus      ; set minus flag
.loop:
   move.b (a0)+,d1
   beq.w .end
   subi.b #$30,d1
   mulu.w #10,d0
   add.w d1,d0
   bra.w .loop
.end: tas.b .minus
   beq.w .exit
   neg.w d0
.exit:  rts

.minus: dc.b 0
   even


; itoa(), integer to alpha (signed)
; hexa conversion routine, d0.w input
;
tohex:
   tst.w d0
   bmi.w .neg
   move.b #' ',hexstr
   bra.w .pos
.neg:  neg.w d0
   move.b #'-',hexstr
.pos:
   lea hexval,a0
   move.l #"0000",(a0)+
.loop:
   moveq #$f,d1
   and.w d0,d1
   move.b hextbl(pc,d1),-(a0)
   lsr.w #4,d0
   bne.w .loop
   rts

hexstr: dc.b "-$"
hexval: dc.b 0,0,0,0,0
hextbl: dc.b "0123456789abcdef"
   even


; itoa(), integer to alpha (unsigned)
; decimal conversion  routine, d0.w input
;
todec:
   lea decval,a0
   move.l #"    ",(a0)+
   move.b #' ',(a0)+
.loop:
   beq.w .end
   divu #10,d0
   swap d0
   move.b #'0',-(a0)
   add.b d0,(a0)
   clr.b d0
   swap d0
   bra.w .loop
.end:  rts

decstr: dcb.b 2,' '
decval: dcb.b 5,' '
        dc.b 0
   even


; print routine, d0.w holds number to print
;
print_hex:      ; write hex value
   bsr.w tohex
   pea hexstr
   bra.w print
print_dec:      ; write dec value
   bsr.w todec
   pea decstr
print:
   move.w #9,-(sp)   ; Cconws, write string to console
   trap #gemdos
   addq.l #6,sp      ; tidy stack
   rts


; Sets palette colour table
;
my_palette:
   move.w #$777,$FFFF8240.w
   move.w #$000,$FFFF825E.w
   rts


;-----------------------------
;   DATA / BSS - variables
;-----------------------------
   data
banner:
   dc.b 27,"b",15,27,"E",27,"p"
   dc.b "-------------------",13,10
   dc.b "Conversion routines",13,10
   dc.b "-------------------",13,10
   dc.b 27,"qatoi(),itoa(),Hex,Dec",13,10
   dc.b "(c)tcat 2018",13,10,13,10,10
   dc.b "Enter number 0..65535, -32768..32767",13,10
   dc.b "Ctrl-C to quit",0
   even

   bss
            ds.l 256
stack:      ds.l 1
   even


CONVERT.zip

Many thanks
Tomas
You do not have the required permissions to view the files attached to this post.

Social Media

     

Return to “680x0”

Who is online

Users browsing this forum: No registered users and 6 guests