17
\$\begingroup\$

Challenge

I've recently gotten into 8-bit computers and are fascinated with the workings of them and others alike; thus the objective of this code golf, is to replicate a portion of the Woz Monitor, designed by Steve Wozniak for the Apple I.

You are to store an array of 22 hexadecimal values with a width of two bytes, (min value $10, max value $FF), and then take in n-amount of inputs. (Normally two; languages like Brainfuck might have a hard time).
The inputs will refer to where in the array to start printing from, and where to stop; an input with defined behaviour will have their starting value less or equal to the ending value. Your program must then be capable of printing every hexadecimal value between, and including, the hexadecimals inputted.

An example of this:

Array
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15

Values
FF F4 B6 D7 40 00 00 A4 F0 00 FF 0D 9C 21 65 C4 02 28 90 E7 D6 A5

input first num:
04
input second num:
14
40 00 00 A4 F0 00 FF 0D 9C 21 65 C4 02 28 90 E7 D6

Now the interesting part of this exercise, is that you can use whatever you want to check the bounds of the users input. Person inputs hello and your program has undefined behaviour? It quits without notice? They are both valid.

The only rules are:

1. You must include the values of the 22 hexadecimal values as part of your program before it starts, (can't ask the user for inputs).
2. The output of the hexadecimal values must follow the exact format: 00 FF 00 FF 00 Trailing spaces, tabs or lines are OK. Characters are not.
3. The program doesn't have to ask for the inputs with a message. Leave the "message" blank if you wish. The user must input the hex-bounds however.
4. As with the values of the 22 hexadecimals are up to you to decide, you must make a program that actually fetches the values from storage, opposed to mimicking a program by simply printing values. (such as a list of $00's).
5. n-amount of inputs, refers to the amount of inputs required for your language of choice to recognise a hexadecimal of two byte-width. eg. (Brainfuck will require two inputs per hex, making it four for the two).

Feel free to comment if you need clarification.

This is code golf, so the shortest answer in the number of bytes is the winner.

Leaderboard

Here is a leaderboard generating snippet courtesy of Martin Ender.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

function answersUrl(e){return"http://api.stackexchange.com/2.2/questions/95080/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function getAnswers(){$.ajax({url:answersUrl(page++),method:"get",dataType:"jsonp",crossDomain:true,success:function(e){answers.push.apply(answers,e.items);if(e.has_more)getAnswers();else process()}})}function shouldHaveHeading(e){var t=false;var n=e.body_markdown.split("\n");try{t|=/^#/.test(e.body_markdown);t|=["-","="].indexOf(n[1][0])>-1;t&=LANGUAGE_REG.test(e.body_markdown)}catch(r){}return t}function shouldHaveScore(e){var t=false;try{t|=SIZE_REG.test(e.body_markdown.split("\n")[0])}catch(n){}return t}function getAuthorName(e){return e.owner.display_name}function process(){answers=answers.filter(shouldHaveScore).filter(shouldHaveHeading);answers.sort(function(e,t){var n=+(e.body_markdown.split("\n")[0].match(SIZE_REG)||[Infinity])[0],r=+(t.body_markdown.split("\n")[0].match(SIZE_REG)||[Infinity])[0];return n-r});var e={};var t=0,c=0,p=-1;answers.forEach(function(n){var r=n.body_markdown.split("\n")[0];var i=$("#answer-template").html();var s=r.match(NUMBER_REG)[0];var o=(r.match(SIZE_REG)||[0])[0];var u=r.match(LANGUAGE_REG)[1];var a=getAuthorName(n);t++;c=p==o?c:t;i=i.replace("{{PLACE}}",c+".").replace("{{NAME}}",a).replace("{{LANGUAGE}}",u).replace("{{SIZE}}",o).replace("{{LINK}}",n.share_link);i=$(i);p=o;$("#answers").append(i);e[u]=e[u]||{lang:u,user:a,size:o,link:n.share_link}});var n=[];for(var r in e)if(e.hasOwnProperty(r))n.push(e[r]);n.sort(function(e,t){if(e.lang>t.lang)return 1;if(e.lang<t.lang)return-1;return 0});for(var i=0;i<n.length;++i){var s=$("#language-template").html();var r=n[i];s=s.replace("{{LANGUAGE}}",r.lang).replace("{{NAME}}",r.user).replace("{{SIZE}}",r.size).replace("{{LINK}}",r.link);s=$(s);$("#languages").append(s)}}var QUESTION_ID=49042;var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";var answers=[],page=1;getAnswers();var SIZE_REG=/\d+(?=[^\d&]*(?:&lt;(?:s&gt;[^&]*&lt;\/s&gt;|[^&]+&gt;)[^\d&]*)*$)/;var NUMBER_REG=/\d+/;var LANGUAGE_REG=/^#*\s*([^,]+)/
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src=https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js></script><link rel=stylesheet type=text/css href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"><div id=answer-list><h2>Leaderboard</h2><table class=answer-list><thead><tr><td></td><td>Author<td>Language<td>Size<tbody id=answers></table></div><div id=language-list><h2>Winners by Language</h2><table class=language-list><thead><tr><td>Language<td>User<td>Score<tbody id=languages></table></div><table style=display:none><tbody id=answer-template><tr><td>{{PLACE}}</td><td>{{NAME}}<td>{{LANGUAGE}}<td>{{SIZE}}<td><a href={{LINK}}>Link</a></table><table style=display:none><tbody id=language-template><tr><td>{{LANGUAGE}}<td>{{NAME}}<td>{{SIZE}}<td><a href={{LINK}}>Link</a></table>

\$\endgroup\$
  • \$\begingroup\$ Are we asking the user for two inputs or n? Do we choose what the 22 hex values are? \$\endgroup\$ – xnor Oct 1 '16 at 8:49
  • \$\begingroup\$ Yes, the 22 values are yours to decide. The input values, they can be any amount. I said n because Brainfuck can't take in a 2 character string, you would need to input the first byte, then the second for the first value, and then do it again for the second value, 4 inputs in total. They can be however many as you like. \$\endgroup\$ – finnrayment Oct 1 '16 at 8:53
  • \$\begingroup\$ However with the values, you can't simply have them all as 00, and have a program mimic what something would that actually reads the arrays. Updating the question. \$\endgroup\$ – finnrayment Oct 1 '16 at 8:53
  • \$\begingroup\$ Should it wrap or read in reverse if input 2 is less than input 1? \$\endgroup\$ – Jonathan Allan Oct 1 '16 at 9:02
  • \$\begingroup\$ @JonathanAllan That is entirely up to you. Just remember, making sure the inputs are correct may make your code bigger. As written just above the rules, you can allow any form of input (up to you), leading to undefined behaviour or errors if you wish. \$\endgroup\$ – finnrayment Oct 1 '16 at 9:05

12 Answers 12

4
\$\begingroup\$

Jelly, 24 21 bytes

w@€ØHḅ⁴
ɠǵɠÇr@b⁴ịØHK

Chosen values: [00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 1F 10 11 12 13 14 15]

TryItOnline

If the second input is less than the first it outputs the reversed sequence.
Out of bounds behaviour is undefined (e.g. "foo", "14" returns 38 values, most of which are not even in the array and many of which are not length 2)

How?

w@€ØHḅ⁴ - Link 1, parse a string as a 1-based hex value e.g. "14"
w@€      - first index (1-based) of each character in
   ØH    - hex digits: "0123456789ABCDEF"                   [2,5]
     ḅ⁴ - convert from base 16                                 37

ɠǵɠÇr@b⁴ịØHK - Main link
ɠ  ɠ          - read a line from stdin               e.g. "04"  "14"
 Ç  Ç         - call the last link (1) as a monad          21    37
  µ           - monadic chain separation
     r@       - inclusive range, with reversed arguments  [   21,   22,...,   36,   37] 
       b⁴     - convert to base 16                        [[1,5],[1,6],...,[2,4],[2,5]]
         ị    - index into
          ØH  - hex digits: "0123456789ABCDEF"            [ "04", "05",..., "13", "14"]
            K - join with spaces
\$\endgroup\$
  • \$\begingroup\$ Ah, pardon me. You were indeed correct. Well done, and have leaderboard position #1. :) \$\endgroup\$ – finnrayment Oct 1 '16 at 10:20
  • 1
    \$\begingroup\$ yes, I just moved and changed the one offset value (now 1F), meaning I did not need to "correctly" interpret the hex inputs or add 16 to give me two hex digits to convert back. \$\endgroup\$ – Jonathan Allan Oct 1 '16 at 11:49
4
\$\begingroup\$

JavaScript (ES6), 118 115 112 102 82 81 bytes

Saved 1 byte thanks to ETHproductions

Chosen values:

00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15
-----------------------------------------------------------------
10 10 11 14 10 10 15 15 11 14 10 10 15 15 11 14 10 10 15 15 10 11
  • Prompts for the lower bound, then for the upper bound (e.g. 0x04 / 0x0f).
  • An invalid lower bound will be interpreted as 0x00 (minimum value).
  • An invalid upper bound will be interpreted as 0x15 (maximum value).
  • Outputs nothing if the lower bound is greater than the upper bound.

alert((1/7+'789').replace(/./g,(v,i)=>i<a|i>b?'':(v|10)+' ',p=prompt,b=p(a=p())))

Previous version (97 bytes)

Generating a pseudo-random list of 'true' hexadecimal values:

alert((1/7+'789').replace(/./g,(v,i)=>i<a|i>b?'':(v*7|16).toString(16)+' ',p=prompt,a=p(),b=p()))

Sequence:

10 10 17 1c 1e 38 33 31 17 1c 1e 38 33 31 17 1c 1e 38 33 31 38 3f
\$\endgroup\$
  • \$\begingroup\$ "message": "Uncaught SyntaxError: Invalid destructuring assignment target" \$\endgroup\$ – finnrayment Oct 1 '16 at 10:57
  • \$\begingroup\$ @frayment - This is Chrome, right? That's weird because it doesn't complain about [a,b]=prompt().split(' ') in the command line. Anyway, my updated answer should fix that. \$\endgroup\$ – Arnauld Oct 1 '16 at 11:08
  • \$\begingroup\$ Well done! Works now. Thats weird that Chrome does it. I tested it on your JS tester, and in my Developer console. Same error. Welcome to the leaderboard. \$\endgroup\$ – finnrayment Oct 1 '16 at 11:09
  • \$\begingroup\$ You could save some bytes with inputs like 0x04 \$\endgroup\$ – Hedi Oct 1 '16 at 11:15
  • 1
    \$\begingroup\$ Dude, theres nothing wrong with your alternate, it works fine here and is within the rules. Update your post! :) \$\endgroup\$ – finnrayment Oct 1 '16 at 12:58
3
\$\begingroup\$

JavaScript (ES6), 107 152 137 bytes

p=prompt,f=(a=+p(),b=+p(),[x,y,...s]='FFF4B6D7402011A4F0D0FF3D9C2165C4A22890E7D6A5')=>a?f(a-1,b-1,s):x+y+' '+(b?f(a,b-1,s):'');alert(f())

var
   p=prompt,f=(a=+p(),b=+p(),[x,y,...s]='FFF4B6D7402011A4F0D0FF3D9C2165C4A22890E7D6A5')=>a?f(a-1,b-1,s):x+y+' '+(b?f(a,b-1,s):'');alert(f())
;

//input 0x04 then 0x14 will print 40 to D6
//input 0x00 then 0x00 will print FF
//input 0x13 then 0x17 will print E7 D6 A5 NaN NaN

Chosen values:

FF F4 B6 D7 40 20 11 A4 F0 D0 FF 3D 9C 21 65 C4 A2 28 90 E7 D6 A5

Inputs:

  • Input format is 0x14
  • If any input is negative or the 1st input is greater than the 2nd : InternalError: too much recursion
  • Will print NaN for out of bounds.

Previous solutions:
152 bytes:

i=parseInt,p=prompt,f=(a=i(p()),b=i(p()),[x,y,...s]='FFF4B6D7402011A4F0D0FF3D9C2165C4A22890E7D6A5')=>a?f(a-1,b-1,s):x+y+' '+(b?f(a,b-1,s):'');alert(f())

107 bytes, invalid solution (inputs missing):

f=(a,b,[x,y,...s]='FFF4B6D7402011A4F0D0FF3D9C2165C4A22890E7D6A5')=>a?f(a-1,b-1,s):x+y+' '+(b?f(a,b-1,s):'')
\$\endgroup\$
  • 1
    \$\begingroup\$ Close! But you need to be able to ask the user for input. I like what you did with console.log(...) though. ;) \$\endgroup\$ – finnrayment Oct 1 '16 at 10:05
  • \$\begingroup\$ @frayment var and the last ; are not part of the answer. Its just for the snippet, that's why I added linebreak. I'll edit the answer. \$\endgroup\$ – Hedi Oct 1 '16 at 10:46
  • \$\begingroup\$ My bad @Hedi, sorry about that. \$\endgroup\$ – finnrayment Oct 1 '16 at 10:47
2
\$\begingroup\$

Python, 88 87 86 bytes

1 byte save thanks to @JonathanAllan
1 byte save again to @JonathanAllan

Also changed the base of the code, much nicer now.

a,b=[int(x,16)for x in raw_input().split()];l=[0]*22
while a<=b:print"%02x"%l[a],;a+=1

Chosen values: 00 for everything.

Same idea as my C answer. This time however, the code takes a single input from the user, requiring a space between both values, split them, cast them to hex values, and print every hex in the l array including and between the two inputted values. Since Python has a magnificent error-catching system, no buffer overflows are present and the code is therefore much more secure. The program is safe from undefined behaviour in a sense that it will not execute when a higher value is submitted before a lower value.

This should work on both Python 2.x and 3.x; Please correct me if I am wrong, as I don't have access to both interpreters due to my system not supporting both of them.

\$\endgroup\$
2
\$\begingroup\$

C++, 98 95 93 bytes

#include <iostream>
int _[22],a,b;int main(){for(std::cin>>a>>b;b/a++;)printf("%02x ",_[a]);}

My chosen values are all 0's

\$\endgroup\$
  • \$\begingroup\$ Welcome to Programming Puzzles & Code Golf! This is a well-golfed answer, but I don't think it meets the requirements of the challenge. Would you mind including your 22 chosen hexadecimal values? \$\endgroup\$ – ETHproductions Oct 2 '16 at 1:40
  • \$\begingroup\$ Nearly there! The only problem is, when I entered values 04 and 06, I only got two values back. I'm suspecting these are the 05 and 06 values. You need to provide all values between and including the inputted values. \$\endgroup\$ – finnrayment Oct 2 '16 at 23:46
  • 1
    \$\begingroup\$ @frayment, oh that's right, fixed it! \$\endgroup\$ – Fatih BAKIR Oct 3 '16 at 11:24
1
\$\begingroup\$

Perl, 79 45 41 bytes

"min value $10" - the example has minimum of $00- is that a typo?

Here's a rather boring perl answer in 41 bytes (Was 46 and then I kept seeing spaces, parens I could elide). Takes input in two lines.

printf'%02X ',$_ for(4..26)[hex<>..hex<>]

The data is a list 04..1A

Before I was being too clever with pack & unpack. Its input bytes are entered at once smushed together, eg "020E' will print the 2nd-14th entries

printf'%02X ',$_ for sub{(4..26)[shift..shift]}->(unpack'CC',pack'H4',<>)

Might try golfing it more using all 0's, substr, and printf'%*vX' ... nope that is making my answer longer. 48 characters (using a string of ascii '7', hex 37 as the data)

printf'%*vX',' ',substr 7x22,$s=hex<>,1+hex<>-$s
\$\endgroup\$
1
\$\begingroup\$

CJam, 22 bytes

{r:~Gb}2*37m!s2/\)<>S*

Chosen values:

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15
-----------------------------------------------------------------
13 76 37 53 09 12 26 34 50 46 31 59 79 58 15 80 90 24 00 00 00 00

Try it online

\$\endgroup\$
1
\$\begingroup\$

Scala, 45 bytes

(_:Int)to(_:Int)map(x=>f"$x%02X")mkString " "

Ungolfed:

(a:Int,b:Int)=>a.to(b).map(x=>f"$x%02X").mkString(" ")

Uses 00 to FF as values, but works up to 2147483647.

\$\endgroup\$
  • \$\begingroup\$ error: ')' expected but string literal found. at a.to(b).map( \$\endgroup\$ – finnrayment Oct 2 '16 at 23:56
  • \$\begingroup\$ Don't know if it's just me or what. :/ What should I try running it in? \$\endgroup\$ – finnrayment Oct 2 '16 at 23:56
  • \$\begingroup\$ @frayment that's probably because of the string interpolation, which were introduced in Scala 2.10.0 \$\endgroup\$ – corvus_192 Oct 3 '16 at 3:34
  • \$\begingroup\$ So which version did you build this in? 2.9? \$\endgroup\$ – finnrayment Oct 3 '16 at 4:44
  • \$\begingroup\$ @frayment I used the 2.11.7 REPL \$\endgroup\$ – corvus_192 Oct 3 '16 at 4:56
1
\$\begingroup\$

C, 176 175 161 bytes

1 byte save thanks to @JonathanAllan
Massive help thanks to @Downvoter for saving me 14 bytes!

int main(){int a[44]={0};char s[2];scanf("%s",s);int b=(int)strtol(s,0,16);scanf("%s",s);int c=(int)strtol(s,0,16);while(b<=c){printf("%d%d ",a[b],a[b+1]);b++;}}

Try it online!

Chosen values: 00 for everything.

Un-golfed answer:

int main() {
    int a[44] = {0};
    char s[2];
    scanf("%s", s);
    int b = (int) strtol(s, 0, 16);
    scanf("%s", s);
    int c = (int) strtol(s, 0, 16);
    while (b <= c) {
        printf("%d%d ", a[b], a[b+1]);
        b++;
    }
}

The trick to this, is taking in two inputs and attempting to convert them to hexadecimal strings and then cast to integers. Since there is no error-checking or anything likewise, undefined behaviour is simply throwing errors and breaking the program. The user needs to enter two inputs, however my Eclipse CDT compiler seems to let me input them both on the same line with a space between.

They must be in the correct order, as requesting a bigger value before a smaller value will not run the while-loop at all.

The thing is, there is also no buffer-overflow protection, so I can simply request something absurd such as a range from $0 to $FFFF, and I will get everything in my computers memory from the beginning of the allocation of memory for the a[44] array, all the way to 65536 values later.

\$\endgroup\$
  • \$\begingroup\$ A single whitespace character in the input format matches any amount of whitespace in the input stream, although most scanf formats skip whitespace anyway, speaking of which, why not cut out the middle man and use %x directly? \$\endgroup\$ – Neil Oct 1 '16 at 16:34
  • \$\begingroup\$ Why not use char s[2] instead of the malloc stuff? Casting malloc's return value isn't necessary in C anyway. \$\endgroup\$ – cadaniluk Oct 2 '16 at 10:39
  • \$\begingroup\$ @Neil If your talking about the printf("%d%d ", ...) part, by replacing the formatting with simply %x only returns 0's instead of 00's, and doesn't space them out. \$\endgroup\$ – finnrayment Oct 2 '16 at 23:49
  • \$\begingroup\$ @Downvoter Thankyou very much! I didn't think of that. Editing answer now. \$\endgroup\$ – finnrayment Oct 2 '16 at 23:50
  • \$\begingroup\$ No, I'm still talking about the scanf. \$\endgroup\$ – Neil Oct 3 '16 at 0:08
1
\$\begingroup\$

GNU sed, 209 + 1(r flag) = 210 bytes

1{h
s:.*:,00 FF,01 F4,02 B6,03 D7,04 40,05 00,06 00,07 A4,08 F0,09 00,0A FF,0B 0D,0C 9C,0D 21,0E 65,0F C4,10 02,11 28,12 90,13 E7,14 D6,15 A5:
H;d}
G;s:\n(.*)\n.*(,\1.*):\n\2:
s:(.*)\n(.*,\1 ..).*:\2:
s:,..::g

Try it online! One leading space is present in the output, I hope it's allowed.

Run examples:

me@LCARS:/PPCG$ echo -e "06\n0F" | sed -rf table_lookup.sed
 00 A4 F0 00 FF 0D 9C 21 65 C4
me@LCARS:/PPCG$ echo -e "13\n13" | sed -rf table_lookup.sed
 E7

Explanation: the 22 hexadecimal values stored are the same as the ones from the OP's example

value | FF F4 B6 D7 40 00 00 A4 F0 00 FF 0D 9C 21 65 C4 02 28 90 E7 D6 A5
-------------------------------------------------------------------------
index | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15

The start and end indexes are read on separate lines. The output is a single line with the table values in that index range (inclusive). Undefined input will write multiple lines of invalid output.

# store START index in hold space
1{h
# generate the table
s:.*:,INDEX1 VALUE1,INDEX2 VALUE2,°°°:
# append table to hold space and delete pattern space
H;d}
# read END index, append hold space (pattern space format is: END\nSTART\nTABLE)
G
# delete table entries up to, but excluding, the START index (END\nTABLE')
s:\n(.*)\n.*(,\1.*):\n\2:
# delete table entries starting from, but excluding, the END index (TABLE'')
s:(.*)\n(.*,\1 ..).*:\2:
# remove the indexes and print (implicitly) the resulting values
s:,..::g
\$\endgroup\$
1
\$\begingroup\$

PHP, 106 105 104 96+2 bytes

eval($s='for($a=a;$a++<c;)$$a=hexdec($argv[++$i]);while($b<=$c)printf("%02X ",ord($s[$b++]));');

or

eval($s='for($a=a;$a++<c;)$$a=hexdec($argv[++$i]);');while($b<=$c)printf("%02X ",ord($s[$b++]));

Run with php -nr '<code>' <lowindex> <highindex>; escape the single quotes in the code.
... or test it online.

dechex interpretes input as hexadecimal strings as far as characters are hex digits,
0 if the input starts with something else.

prints nothing if first value is larger than second.

chosen values:

00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15
-----------------------------------------------------------------
66 6F 72 28 24 61 3D 61 3B 24 61 2B 2B 3C 63 3B 29 24 24 61 3D 68

(first 22 ascii codes of the executed code)

or

for($a=a;$a++<c;)eval($s='$$a=hexdec($argv[++$i]);');while($b<=$c)printf("%02X ",ord($s[$b++]));

with these values:

00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15
-----------------------------------------------------------------
24 24 61 3D 68 65 78 64 65 63 28 24 61 72 67 76 5B 2B 2B 24 69 5D 
\$\endgroup\$
  • \$\begingroup\$ Do answers normally incorporate the <?php chunk? \$\endgroup\$ – finnrayment Oct 1 '16 at 12:48
  • \$\begingroup\$ @frayment not if you use -r. And if I need to store it to a file, I use the short open tag <?. \$\endgroup\$ – Titus Oct 1 '16 at 12:52
  • \$\begingroup\$ Constants a, z, and ProgramingPuzles_CGolf are undefined. Where did ProgramingPuzles_CGolf come from anyway? :/ \$\endgroup\$ – finnrayment Oct 1 '16 at 13:08
  • \$\begingroup\$ @frayment Those are notices. Reroute stderr to /dev/null if you don´t like them. PHP evaluates the undefined constants to strings. \$\endgroup\$ – Titus Oct 1 '16 at 13:32
  • \$\begingroup\$ But their coming up as Syntax Errors, I can't run the program. \$\endgroup\$ – finnrayment Oct 1 '16 at 13:37
1
\$\begingroup\$

Apple II 6502 assembly, 75 bytes

Byte code:

A9 46 85 36 A9 10 85 37 A0 00 98 20 DA FD A9 A0 
20 ED FD C0 42 D0 F3 20 93 FE A2 FC 20 1B FD 9D 
04 01 E8 D0 F7 86 31 A9 8D 8D 04 02 20 A7 FF B5 
3C 0A 75 3C 95 3C CA 10 F6 A6 3D BD 05 02 20 ED 
FD E8 E4 3C D0 F5 99 05 02 C8 60

Disassembly:

  LDA    #<+
  STA    CSWL
  LDA    #>+
  STA    CSWH    ;redirect stdout
  LDY    #$00
- TYA
  JSR    PRBYTE  ;print number
  LDA    #$A0    ;space
  JSR    COUT    ;print space
  CPY    #$42    ;22*3
  BNE    -
  JSR    SETVID  ;restore stdout
  LDX    #$FC
- JSR    KEYIN   ;fetch a key
  STA    $0104,X ;store to $200+
  INX
  BNE    -       ;four keys
  STX    MODE    ;set internal flags
  LDA    #$8D
  STA    $0204   ;set key delimiter
  JSR    GETNUM  ;convert keys to hex values
- LDA    A1L,X   ;fetch value
  ASL
  ADC    A1L,X   ;multiply by 3
  STA    A1L,X   ;store value
  DEX
  BPL    -       ;both inputs
  LDX    A1H     ;first input
- LDA    $0205,X ;fetch from index
  JSR    COUT    ;print character
  INX
  CPX    A1L
  BNE    -       ;until second input
+ STA    $0205,Y ;fall through to save a byte
  INY
  RTS

It forms an array in memory that looks like the output. The chosen values are:

00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15
-----------------------------------------------------------------
00 03 06 09 0C 0F 12 15 18 1B 1E 21 24 27 2A 2D 30 33 36 39 3C 3F

User presses four keys to set the inputs.

\$\endgroup\$
  • \$\begingroup\$ Oooo using Apples own code. Very nicely done. \$\endgroup\$ – finnrayment Mar 18 '18 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.