6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri May 03, 2024 10:32 am

All times are UTC




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Byte splitting
PostPosted: Sat Aug 09, 2014 10:17 pm 
Offline

Joined: Sat Aug 09, 2014 9:45 pm
Posts: 5
Hi! New here, seems like a nice forum. I am learning 6502 assembler and using it for NES and C64 coding. I am 30 by the way :)
Already posted this question on NESDEV, but someone with specific skills in 6502 code might have a different view on this.

Ok, so I made this routine to split a byte into two bytes. For example, a value of $C3 would be split up into $0C, and $03. Very handy for hex to text/numeral conversion.

Code:
LDA $input
ROR
ROR
ROR
ROR
AND #$0F
STA $firstbyte_output
 
LDA $input
AND #$0f
STA $secondbyte_output


Is there any way of reducing this routine into fewer operations?

EDIT: Except from TAX or TYA-TXA or TAY..


Top
 Profile  
Reply with quote  
 Post subject: Re: Byte splitting
PostPosted: Sat Aug 09, 2014 11:04 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3350
Location: Ontario, Canada
Welcome, mikaelmoizt! :) Fewer operations, eh. Does it have to be economical with memory use? Lookup tables can cost a lot of memory -- it varies -- but it's a very powerful approach, and often the fastest (fewest operations). In this case the lookup just outputs a masked, shifted version of the input. But far more complex translations are possible. Basically you pre-compute whatever results you want and make a table to hold.them.

cheers,
Jeff
Code:
; the code

LDX $input
LDA LookupTable,X
STA $firstbyte_output
TXA
AND #$0F
STA $secondbyte_output

;the lookup table

LookupTable:
 DB $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
 DB $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01
 DB $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02
 DB $03, $03, $03, $03, $03, $03, $03, $03, $03, $03, $03, $03, $03, $03, $03, $03
 DB $04, $04, $04, $04, $04, $04, $04, $04, $04, $04, $04, $04, $04, $04, $04, $04
 DB $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05, $05
 DB $06, $06, $06, $06, $06, $06, $06, $06, $06, $06, $06, $06, $06, $06, $06, $06
 DB $07, $07, $07, $07, $07, $07, $07, $07, $07, $07, $07, $07, $07, $07, $07, $07
 DB $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08, $08
 DB $09, $09, $09, $09, $09, $09, $09, $09, $09, $09, $09, $09, $09, $09, $09, $09
 DB $0a, $0a, $0a, $0a, $0a, $0a, $0a, $0a, $0a, $0a, $0a, $0a, $0a, $0a, $0a, $0a
 DB $0b, $0b, $0b, $0b, $0b, $0b, $0b, $0b, $0b, $0b, $0b, $0b, $0b, $0b, $0b, $0b
 DB $0c, $0c, $0c, $0c, $0c, $0c, $0c, $0c, $0c, $0c, $0c, $0c, $0c, $0c, $0c, $0c
 DB $0d, $0d, $0d, $0d, $0d, $0d, $0d, $0d, $0d, $0d, $0d, $0d, $0d, $0d, $0d, $0d
 DB $0e, $0e, $0e, $0e, $0e, $0e, $0e, $0e, $0e, $0e, $0e, $0e, $0e, $0e, $0e, $0e
 DB $0f, $0f, $0f, $0f, $0f, $0f, $0f, $0f, $0f, $0f, $0f, $0f, $0f, $0f, $0f, $0f

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
 Post subject: Re: Byte splitting
PostPosted: Sun Aug 10, 2014 12:17 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8430
Location: Southern California
mikaelmoizt, you can eliminate your first AND#$0F if you use LSR instead of ROR, because LSR shifts zeros in from the left.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: Byte splitting
PostPosted: Sun Aug 10, 2014 12:40 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8172
Location: Midwestern USA
mikaelmoizt wrote:
Code:
LDA $input
ROR
ROR
ROR
ROR
AND #$0F
STA $firstbyte_output
 
LDA $input
AND #$0f
STA $secondbyte_output

Try this:
Code:
         LDA $input
         PHA
         LSR A
         LSR A
         LSR A
         LSR A
         STA $firstbyte_output
         PLA
         AND #$0f
         STA $secondbyte_output


BTW, most assemblers would not recognize $input as valid syntax.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
 Post subject: Re: Byte splitting
PostPosted: Sun Aug 10, 2014 5:17 am 
Offline

Joined: Sun Jun 29, 2014 5:42 am
Posts: 337
Hi Mike,

Here's a complete example from the Acorn Atom Kernel ROM:
Code:
  Print a Byte in Hexadecimal subroutine
  --------------------------------------

- Prints the contents of the accumulator in hex
- X and Y registers preserved.

F802  48        PHA             Save number
F803  4A        LSR A           (
F804  4A        LSR A           (
F805  4A        LSR A           ( Shift the upper nibble down to the
F806  4A        LSR A           ( lower
F807  20 0B F8  JSR #F80B       Print nibble in hex via OSWRCH
F80A  68        PLA             Restore number
F80B  29 0F     AND @#F         Clear upper nibble
F80D  C9 0A     CMP @#A         Is it a letter ?
F80F  90 02     BCC #F813       ..yes
F811  69 06     ADC @#6         ..no, it's a numeric digit
F813  69 30     ADC @#30        Convert to ASCII
F815  4C F4 FF  JMP #FFF4       ..and print via OSWRCH

Dave


Top
 Profile  
Reply with quote  
 Post subject: Re: Byte splitting
PostPosted: Sun Aug 10, 2014 10:52 am 
Offline

Joined: Sat Aug 09, 2014 9:45 pm
Posts: 5
Thanks a lot everyone for the massive response!

The lookup table is a great idea, I will use it when I got more memory to spare and if the project I am working on shows slowdown tendencies.
I have a project where numerals and text are separated with special characters inbetween, so I will have to use lookup table there.
Post by hoglet gave me an idea to maybe combine methods later on.

BigDumbDinosaur wrote:
BTW, most assemblers would not recognize $input as valid syntax.


Nor mine, the "$" was there for the purpose of showing adress loading instead of immediate loading.. guess it was a bit excessive :)
The code got a bit cleaned up and fixed like this.

Code:
; In case we got a standard charset setup 0..9 and letters A-F following.
         LDA input
         PHA
         LSR ; So I finally start to understand how this opcode works..
         LSR
         LSR
         LSR
         ; ADC offset
         STA firstbyte_output
         PLA
         AND #$0f
         ; ADC offset
         STA secondbyte_output


Offtopic: I really like this sort of "old and not so fancy" technology. I read somewhere in this forum that younger people tend to need instant satisfaction and all high tech.
It is true, and sad. Back when I was younger, using a computer was a learning process with all the thinking and some hard work in order to make something usefull or fun.
Learning something new (or old as it might be) is stimulating no matter who you are. It just takes some effort and thinking, that's all. Also, I'm from sweden (hej!) so please excuse my 8th grade english 8)


Top
 Profile  
Reply with quote  
 Post subject: Re: Byte splitting
PostPosted: Sun Aug 10, 2014 11:26 am 
Offline

Joined: Mon Aug 05, 2013 10:43 pm
Posts: 258
Location: Southampton, UK
Quote:
Offtopic: I really like this sort of "old and not so fancy" technology


Agree. I do modern development for a living (linux platforn thankfully). What I love about the "old" stuff is you have opportunity to understand the system *completely* if you are willing. Something that is impossible in modern computing. This is the core reason I'm building my own 8 bit. The next level, if course, is to implement my own MPU but I won't be at that level any time soon.

PS. Welcome!

_________________
8 bit fun and games: https://www.aslak.net/


Last edited by Aslak3 on Sun Aug 10, 2014 3:56 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Byte splitting
PostPosted: Sun Aug 10, 2014 3:07 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8430
Location: Southern California
mikaelmoizt, on your ADC lines that are commented out, if you do un-comment them, be sure to put the CLC before the first one, since the carry flag's status will not be known. It will be whatever LSR shifted out last in this case. (Depending on your first offset, I think C will always be clear for the second one.)

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: Byte splitting
PostPosted: Mon Aug 11, 2014 2:59 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8172
Location: Midwestern USA
mikaelmoizt wrote:
Code:
; In case we got a standard charset setup 0..9 and letters A-F following.
         LDA input
         PHA
         LSR ; So I finally start to understand how this opcode works..
         LSR
         LSR
         LSR
         ; ADC offset
         STA firstbyte_output
         PLA
         AND #$0f
         ; ADC offset
         STA secondbyte_output

Following the four LSR A (accumulator mode, which may have to be signified in your assembler) instructions, but immediately before the ADC offset, you should insert a CLC. As LSR shifts bits, bit 0 will be transferred to the carry bit in the status register. If bit 3 of whatever was loaded from input is set, carry will be set after the fourth shift, causing an error-by-one when you add the offset to the accumulator.

Quote:
Offtopic: I really like this sort of "old and not so fancy" technology. I read somewhere in this forum that younger people tend to need instant satisfaction and all high tech. It is true, and sad. Back when I was younger, using a computer was a learning process with all the thinking and some hard work in order to make something usefull or fun. Learning something new (or old as it might be) is stimulating no matter who you are. It just takes some effort and thinking, that's all.

Periodically, we have...er...vigorous discussion in the forum about the role the Arduinos and Raspberry PIs of the world have in our hobby. I and a few others ( :wink: ) maintain that using these devices deprives the hobbyist of the opportunity to learn about the digital machinery that makes computers work, and is indeed a form of instant gratification. Others don't see it that way, of course. :lol:

Quote:
Also, I'm from sweden (hej!) so please excuse my 8th grade english 8)

I have to tell you that your written English is better than a lot of what I see put forth by Americans, who are native speakers and are supposed to be able to use proper spelling, grammar and punctuation.

Where are you located in Sweden?

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
 Post subject: Re: Byte splitting
PostPosted: Mon Aug 11, 2014 3:52 pm 
Offline

Joined: Sat Aug 09, 2014 9:45 pm
Posts: 5
Quote:
Periodically, we have...er...vigorous discussion in the forum about the role the Arduinos and Raspberry PIs of the world have in our hobby. I and a few others ( :wink: ) maintain that using these devices deprives the hobbyist of the opportunity to learn about the digital machinery that makes computers work, and is indeed a form of instant gratification. Others don't see it that way, of course. :lol:


I see :)
Not everyone likes to learn (or even like to have the option to learn) how things work internally I guess. I, myself could sit just staring for hours at some very long code listing of a program or game inside a monitor or debugger (or even hexeditor some times) trying to understand how, why and when things happen. It is all part of the fun in seeing what is really going on.
That ofcourse applies to understanding some parts of the hardware itself aswell.

Even further away from the user than RPi & Arduino (and possibly not in the same category, but nonetheless programmed by someone in something) are these things we call "Apps". Yes for our smartphones.
I never had so much processing power in my hand, yet - my flashlight app takes 4,12 megabytes..? And it is a.. light. With an onscreen on/off button. How could that possibly be? :roll:

I live in a small town near Gothenburg (30 km's). The west coast is the best coast as we say here :wink:


Top
 Profile  
Reply with quote  
 Post subject: Re: Byte splitting
PostPosted: Mon Aug 11, 2014 11:57 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8172
Location: Midwestern USA
mikaelmoizt wrote:
I live in a small town near Gothenburg (30 km's). The west coast is the best coast as we say here :wink:

Ah, yes, the name rings a bell. Way back when when I was in the US Navy, we steamed about 20 miles off shore past Gothenburg,. I don't recall the exact reason why we were in the neighborhood, but we were part of a fairly large-scale NATO exercise involving the UK, Germany and France. We spent quite a bit of time bouncing around the North Sea. :lol:

After the exercise was over, we steamed up to Bergen in Norway and then over the top of Norway into the Barents sea with a brief stop at Menhamn. It was during the late winter months and was a cold trip.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
 Post subject: Re: Byte splitting
PostPosted: Tue Aug 12, 2014 2:55 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1927
Location: Sacramento, CA, USA
BigDumbDinosaur wrote:
... [emphasis added] ...
... we steamed up to Bergen in Norway and then over the top of Norway into the Barents sea with a brief stop at Menhamn. It was during the late winter months and was a cold trip.


Wow! Your user name isn't as far from the actual truth as I thought it was, at least with regard to your age. :wink:
Did you guys argue over who was going to spend the most time in the warm boiler-room, shoveling coal?

BTW, here's how Woz did it, 38 years ago, before the 6502 even had an ROR instruction: (Look ma! No CLC!)
Code:
;-------------------------------------------------------------------------
;  Subroutine to print a byte in A in hex form (destructive)
;-------------------------------------------------------------------------

PRBYTE          PHA                     Save A for LSD
                LSR
                LSR
                LSR                     MSD to LSD position
                LSR
                JSR     PRHEX           Output hex digit
                PLA                     Restore A

; Fall through to print hex routine

;-------------------------------------------------------------------------
;  Subroutine to print a hexadecimal digit
;-------------------------------------------------------------------------

PRHEX           AND     #%0000.1111     Mask LSD for hex print
                ORA     #"0"            Add "0"
                CMP     #"9"+1          Is it a decimal digit?
                BCC     ECHO            Yes! output it
                ADC     #6              Add offset for letter A-F

; Fall through to print routine

;-------------------------------------------------------------------------
;  Subroutine to print a character to the terminal
;-------------------------------------------------------------------------

ECHO            BIT     DSP             DA bit (B7) cleared yet?
                BMI     ECHO            No! Wait for display ready
                STA     DSP             Output character. Sets DA
                RTS


Mike


Top
 Profile  
Reply with quote  
 Post subject: Re: Byte splitting
PostPosted: Tue Aug 12, 2014 4:01 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8172
Location: Midwestern USA
barrym95838 wrote:
BigDumbDinosaur wrote:
... [emphasis added] ...
... we steamed up to Bergen in Norway and then over the top of Norway into the Barents sea with a brief stop at Menhamn. It was during the late winter months and was a cold trip.

Wow! Your user name isn't as far from the actual truth as I thought it was, at least with regard to your age. :wink:
Did you guys argue over who was going to spend the most time in the warm boiler-room, shoveling coal?

Egad, I'm not *that* old. :lol: The US Navy quit firing their boilers with coal during the 1940s. :lol: BTW, the term steamed is used regardless of the type of powerplant fitted to the ship. Also, the Navy still has ships that have steam plants and burn fuel oil.

Quote:
BTW, here's how Woz did it, 38 years ago, before the 6502 even had an ROR instruction: (Look ma! No CLC!)
Code:
;-------------------------------------------------------------------------
;  Subroutine to print a byte in A in hex form (destructive)
;-------------------------------------------------------------------------

PRBYTE          PHA                     Save A for LSD
                LSR
                LSR
                LSR                     MSD to LSD position
                LSR
                JSR     PRHEX           Output hex digit
                PLA                     Restore A

; Fall through to print hex routine

;-------------------------------------------------------------------------
;  Subroutine to print a hexadecimal digit
;-------------------------------------------------------------------------

PRHEX           AND     #%0000.1111     Mask LSD for hex print
                ORA     #"0"            Add "0"
                CMP     #"9"+1          Is it a decimal digit?
                BCC     ECHO            Yes! output it
                ADC     #6              Add offset for letter A-F

; Fall through to print routine

;-------------------------------------------------------------------------
;  Subroutine to print a character to the terminal
;-------------------------------------------------------------------------

ECHO            BIT     DSP             DA bit (B7) cleared yet?
                BMI     ECHO            No! Wait for display ready
                STA     DSP             Output character. Sets DA
                RTS

Here's how I do it in Supermon 816. It's actually eight bit code, so it will work with any 65xx MPU:

Code:
10183    ;================================================================================
10184    ;
10185    ;binhex: CONVERT BINARY BYTE TO HEX ASCII CHARS
10186    ;
10187    ;   ————————————————————————————————————————————
10188    ;   Preparatory Ops: .A: byte to convert
10189    ;
10190    ;   Returned Values: .A: MSN ASCII char
10191    ;                    .X: LSN ASCII char
10192    ;                    .Y: entry value
10193    ;
10194    ;   Calling Example: LDA #$E4
10195    ;                    JSR BINHEX
10196    ;                    JSR BSOUTA     ;display MSN
10197    ;                    TXA
10198    ;                    JSR BSOUTA     ;display LSN
10199    ;   ————————————————————————————————————————————
10200    ;
10201    F792  20 F4 F6     binhex   jsr bintonyb          ;generate binary values
10202    F795  48                    pha                   ;save MSN
10203    F796  8A                    txa
10204    F797  20 9C F7              jsr .0000010          ;generate ASCII LSN
10205    F79A  AA                    tax                   ;save LSN
10206    F79B  68                    pla                   ;get MSN & fall thru
10207    ;
10208    ;
10209    ;   convert nybble to hex ASCII equivalent...
10210    ;
10211    F79C  C9 0A        .0000010 cmp #10
10212    F79E  90 02                 bcc .0000020          ;in decimal range
10213    ;
10214    F7A0  69 66                 adc #k_hex            ;hex compensate
10215    ;         
10216    F7A2  49 30        .0000020 eor #'0'              ;finalize nybble
10217    F7A4  60                    rts                   ;done
10218    ;
10023    ;================================================================================
10024    ;
10025    ;bintonyb: EXTRACT BINARY NYBBLES
10026    ;
10027    ;   —————————————————————————————————
10028    ;   Preparatory Ops: .A: binary value
10029    ;
10030    ;   Returned Values: .A: MSN
10031    ;                    .X: LSN
10032    ;                    .Y: entry value
10033    ;   —————————————————————————————————
10034    ;
10035    F6F4  48           bintonyb pha                   ;save
10036    F6F5  29 0F                 and #bcdumask         ;extract LSN
10037    F6F7  AA                    tax                   ;save it
10038    F6F8  68                    pla
10039                                .rept s_bnybbl        ;extract MSN
10040    F6F9  4A                      lsr
10041    F6FA  4A                      lsr
10042    F6FB  4A                      lsr
10043    F6FC  4A                      lsr
10044    F6FD  60                    rts
10045    ;

The conversion of the byte to nybbles is in a subroutine of its own because the operation is used in several places.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 12, 2014 9:11 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10793
Location: England
Hey BDD, did you realise they had a film crew nearby?
http://youtu.be/xPWGCC_BYE8?t=22s
Only joking... I went up that way myself last year:
https://plus.google.com/107049823915731 ... 5Z6F4fgbvZ

Cheers
Ed


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 12, 2014 9:22 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8172
Location: Midwestern USA
BigEd wrote:
Hey BDD, did you realise they had a film crew nearby?
http://youtu.be/xPWGCC_BYE8?t=22s

Dang! That was a victim of a copyright takedown. :cry:

Quote:
Only joking... I went up that way myself last year:
https://plus.google.com/107049823915731 ... 5Z6F4fgbvZ

Cheers
Ed

Looks as though you got a good view of the Lights. At least you didn't break a leg! :lol:

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 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: