EASy68K  
It is currently Wed Dec 11, 2019 8:38 pm

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: String question
PostPosted: Tue May 26, 2009 1:08 pm 
Offline

Joined: Tue May 26, 2009 8:06 am
Posts: 6
I am curious, it is possible to add variable to string?
for example, my code count how many even number and odd number
and i store even number in d2, odd number in d3.

in my code can i write something like below
string dc.b 'The number of even number is ' + d2
dc.b 'The number of odd number is' +d3

just wondering have such or similar idea to add variable to string

regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 26, 2009 2:17 pm 
Offline

Joined: Fri Dec 19, 2008 1:12 pm
Posts: 37
You can do it like this:
Code:
;
; After counting digits:
;
    move.l  d2,even_digits
    move.l  d3,odd_digits
;
; Rest of your code
;

;
; Variables:
;
even_digits
    dc.l    0
odd_digits
    dc.l    0

In this example it's assumed 32 bits are used for the counting.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 26, 2009 2:23 pm 
Offline

Joined: Tue May 26, 2009 8:06 am
Posts: 6
possible to place it in between the statement instead of printing seperately?

for example even has 4 and odd has 5

so, print out : There were 4 even and 5 odd digits in the string


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 27, 2009 9:17 am 
Offline

Joined: Fri Dec 19, 2008 1:12 pm
Posts: 37
Hehe, I read your first post too quickly and gave you the wrong advice!

Try this code, I think it will do what you need it to:
Code:
    org     $1000
start:
;
; Print number of odd and even digits.
;
    move.l  #123,d2     ;Number of odd digits.
    move.l  #234,d3     ;Number of even digits.
;
; First number
;
    lea     odd_nr,a1   ;Show odd string.
    move.b  #14,d0      ;
    trap    #15         ;

    move.l  d2,d0       ;Convert odd number to decimal ascii.
    bsr     bin_to_dec  ;

    move.b  #13,d0      ;Show number of odd digits.
    trap    #15         ;
;
; Second number
;
    lea     even_nr,a1  ;Show odd string.
    move.b  #14,d0      ;
    trap    #15         ;

    move.l  d3,d0       ;Convert even number to decimal ascii.
    bsr     bin_to_dec  ;

    move.b  #13,d0      ;Show number of odd digits.
    trap    #15         ;
;
; End of program
;
    move.b  #9,d0
    trap    #15      ;Halt simulator
;
; Convert 8 bit binary to decimal.
;
;input:
; d0=8 bit binary number.
;
;output:
; a1=start of number string.
;
bin_to_dec
    movem.l d2-d4,-(sp)

    lea     number,a1
    move.l  #0,(a1)
    addq.l  #3,a1

    moveq   #0,d2
    move.b  d0,d2

    moveq   #2,d4
bin_to_dec_loop
    divu.w  #10,d2
    swap    d2

    move.b  d2,d3
    add.l   #'0',d3
    move.b  d3,-(a1)

    move.w  #0,d2
    swap    d2

    cmp.w   #0,d2
    beq     bin_to_dec_end

    dbra    d4,bin_to_dec_loop
bin_to_dec_end
    movem.l (sp)+,d2-d4
    rts
;
; Strings.
;
number
    dc.l    0
odd_nr
    dc.b    'Number of odd digits:',0
even_nr
    dc.b    'Number of even digits:',0

    end start

A couple of words of advice:

1) The number of digits must not be more than 255. This code only works with 8 bit numbers. If you want more, you must adapt the code yourself.
2) I haven't added any comments to the routine that converts the numbers to decimal ascii. Before you ask how it works, try to figure it out yourself.
3) As always in these cases, try to understand the code, and don't blindly copy it into your code. Trying to understand it will help you learn!

Any questions? Ask them, but only after trying to understand things yourself first :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 28, 2009 11:33 am 
Offline

Joined: Tue May 26, 2009 8:06 am
Posts: 6
oo..by using subroutine, but i dunno wat is lea i go google + ing, anywhere thx u so much. One last question, can we print out 01,02,03.....0E,0F by hexadecimal instead of printing 1....15 and on?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 28, 2009 6:48 pm 
Offline

Joined: Fri Dec 19, 2008 1:12 pm
Posts: 37
poklin wrote:
oo..by using subroutine, but i dunno wat is lea i go google + ing, anywhere thx u so much. One last question, can we print out 01,02,03.....0E,0F by hexadecimal instead of printing 1....15 and on?
No problem, mate :thumbup:

As for your question, here is the routine that converts an 8 bit number to hex:
Code:
;
; Convert 8 bit binary to hexadecimal.
;
;input:
; d0=8 bit binary number.
;
;output:
; a1=start of number string.
;
bin_to_hex
    movem.l d0-d2,-(sp)

    lea     number,a1
    move.l  #0,(a1)
    addq.l  #3,a1

    moveq   #1,d2
bin_to_hex_loop
    move.b  d0,d1
    lsr.b   #4,d0
    and.b   #15,d1
   
    cmp.b   #9,d1
    ble     bin_to_hex_l1

    sub.b   #10,d1
    add.b   #'A',d1
    move.b  d1,-(a1)
    bra     bin_to_hex_next

bin_to_hex_l1
    add.b   #'0',d1
    move.b  d1,-(a1)

bin_to_hex_next
    dbra    d2,bin_to_hex_loop

    movem.l (sp)+,d0-d2
    rts

All you have to do is insert this routine into the code I showed you earlier, and change the bsr instructions to call this routine instead of bin_to_dec. The routine is called in the same way as bin_to_dec, and produces the same kind of output, a pointer.

As for looking up cpu instrucions with Google, let's just say that you really need a 68000 assembler manual. Search for that instead. If you can't find any (do try), then I'm happy to get you one.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 31, 2009 3:45 am 
Offline

Joined: Tue May 26, 2009 8:06 am
Posts: 6
ya, i got the 68000 manual~ty
can i have 1 more question?
my memory had store some data in hexadecimal used by address register
how to i display it out originally with those hexadecimal ? can i use
LOOP MOVE.B (A2)+,D1 ; I store a list of data in a2
BEQ FINISH
MOVE.B #9,D0
TRAP #15
BRA LOOP5


it seem does not work to direclty print out the data store in a2.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 31, 2009 1:30 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1109
Trap task 15 will display the number in D1.L converted to the number base (2 through 36) contained in D2.L. To display a series of 1 byte numbers in hexadecimal do something like the following:

Code:
       ORG   $1000
START:            ; first instruction of program
        MOVE.L  #16,D2      ; set number base for trap task 15
        MOVE.B  #15,D0      ; trap task 15, display number in base
        LEA     DATA,A2     ; A2 points at data

LOOP    MOVE.B  (A2)+,D1    ; get byte of data
        CMPA    #DATAEND,A2 ; is A2 at end of data?
        BHI     EXIT        ; Yes, exit data display
        TRAP    #15         ; execute task 15, display number in base
        BRA     LOOP

EXIT   MOVE.B   #9,D0
       TRAP   #15      ; halt simulator

* Variables and Strings
DATA    DC.B    $FF,$55,$AA,00,$12           ; room for data
DATAEND:                    ; end of data

   END   START      ; last line of source

_________________
Prof. Kelly


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 02, 2009 4:39 am 
Offline

Joined: Tue May 26, 2009 8:06 am
Posts: 6
HMM..PROF, this example u already defined the data
However i was storing it into address register with certain condition
for example, i store my data in a2, my data already stored as hexadecimal format so i did the code below.
MOVE.L #16,D2 ; set number base for trap task 15
MOVE.B #15,D0 ; trap task 15, display number in base
MOVE.B (A2)+,D1 ; get position of smallest
CMP.B #$FF,D1
BEQ FINISH
MOVE.B #OUTCH,D0
TRAP #15
BRA LOOP5
It's not work, did somewhere wrong with my code?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 02, 2009 1:39 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1109
MOVE.B #OUTCH,D0

This line of code is replacing the #15 in D0 before trap #15 is called. Remove it and try the code.

_________________
Prof. Kelly


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


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 post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group