Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
20 May, 2013, 00:39:15 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1] 2 3 4   Go Down
  Print  
Author Topic: ASM Command of the Week -  (Read 2378 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
ralphdspam
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: 14 May, 2013, 09:10:11
Date Registered: 01 February, 2011, 07:58:40
Location: California, USA
Posts: 841


Topic starter
Total Post Ratings: +36

View Profile
« on: 24 December, 2011, 07:27:13 »
+4

The ASM board hasn't been as active as it should be, so I'm starting a weekly discussion called "ASM Command of the Week."  Basically, we will discuss hacks, routines, optimizations, or alternatives to the command.  

It's basically going to be like these topics, except this one is going to be weekly.
DJNZ:
http://www.omnimaga.org/index.php?topic=10972.0
RLD and RRD:
http://www.omnimaga.org/index.php?topic=10161.0
IX and IY:
http://www.omnimaga.org/index.php?topic=10036.0

We'll start with a really easy one.  This week's command is:
XOR
« Last Edit: 24 December, 2011, 07:27:33 by ralphdspam » Logged

ld a, 0
ld a, a
calc84maniac
Epic z80 roflpwner
Coder Of Tomorrow
LV11 Super Veteran (Next: 3000)
*
Offline Offline

Gender: Male
Last Login: 14 May, 2013, 10:02:35
Date Registered: 28 August, 2008, 05:09:05
Location: Right behind you.
Posts: 2735


Total Post Ratings: +373

View Profile
« Reply #1 on: 24 December, 2011, 07:35:24 »
+1

Possibly one of the best uses of XOR is in combination with AND, to act as a bitwise multiplexer.

For example, this uses the bits of C to select bits from A and B: if the bit in C is 1, that bit is selected from A, and if the bit in C is 0, that bit is selected from B.
xor b
and c
xor b
Logged

"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman
Hot_Dog
If you can't find a cat, look for its tail.
Support Staff
LV11 Super Veteran (Next: 3000)
*
Offline Offline

Gender: Male
Last Login: 09 May, 2013, 19:22:43
Date Registered: 28 December, 2009, 18:47:54
Location: Midland, Washington
Posts: 2940


Total Post Ratings: +428

View Profile
« Reply #2 on: 24 December, 2011, 07:45:30 »
0

xor a is probably the most common use, faster than "ld a, 0"

xor is also commonly used to "flip" bits in register A.
Logged

There are people who can speak two languages, and they are called bilingual.  There are people who speak three languages and are therefore trilingual.  Then there are people who speak one language, and these people are called Americans.

ZippyDee
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: 12 May, 2013, 10:03:36
Date Registered: 21 March, 2011, 03:15:07
Location: Yes.
Posts: 704


Total Post Ratings: +73

View Profile
« Reply #3 on: 24 December, 2011, 08:13:39 »
0

You can also use xor to test whether two bits within a byte are the same.

and MASK
jr z, bitsAreTheSame
xor MASK
jr z, bitsAreTheSame
;... otherwise, bits are different

where MASK is a mask of the two bits to compare


With this concept, you can use it to swap two bits within a byte:
ld a, (addr)
and MASK
jr z, doneSwapping
xor MASK
jr z, doneSwapping
ld a, (addr)
xor MASK
ld (addr), a
« Last Edit: 24 December, 2011, 08:20:59 by ZippyDee » Logged

There's something about Tuesday...


Pushpins 'n' stuff...

calc84maniac
Epic z80 roflpwner
Coder Of Tomorrow
LV11 Super Veteran (Next: 3000)
*
Offline Offline

Gender: Male
Last Login: 14 May, 2013, 10:02:35
Date Registered: 28 August, 2008, 05:09:05
Location: Right behind you.
Posts: 2735


Total Post Ratings: +373

View Profile
« Reply #4 on: 24 December, 2011, 08:21:19 »
0

and MASK
jp pe,bitsAreTheSame

* calc84maniac runs
Logged

"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman
ZippyDee
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: 12 May, 2013, 10:03:36
Date Registered: 21 March, 2011, 03:15:07
Location: Yes.
Posts: 704


Total Post Ratings: +73

View Profile
« Reply #5 on: 24 December, 2011, 08:22:00 »
0

Explain how that works please...>.< I've never understood how to use parity.

Edit: After thinking about it that makes sense, but that only allows you to test two bits, whereas my method could be used to test any number (I think). Of course the swapping would only work for two bits at a time...
« Last Edit: 24 December, 2011, 08:26:00 by ZippyDee » Logged

There's something about Tuesday...


Pushpins 'n' stuff...

DrDnar
LV6 Super Member (Next: 500)
******
Offline Offline

Last Login: Yesterday at 23:20:34
Date Registered: 29 October, 2010, 00:08:46
Posts: 458

Total Post Ratings: +76

View Profile
« Reply #6 on: 24 December, 2011, 08:28:37 »
0


1
AND mask
masks out all bits except the two in question. If the remaining two bits are the same, either there will be 8 zeros and no ones, or 2 ones and 6 zeros. Thus, the parity will be even. However, if the remaining two bits are different, 1 will be zero, and 1 will be one. Thus, the parity will be odd.
Logged

"The tools which would teach men their own use would be beyond price."—The Republic
chickendude
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Female
Last Login: 12 May, 2013, 18:15:03
Date Registered: 06 September, 2008, 11:27:30
Posts: 435

Total Post Ratings: +66

View Profile
« Reply #7 on: 26 December, 2011, 20:59:03 »
0

Possibly one of the best uses of XOR is in combination with AND, to act as a bitwise multiplexer.

For example, this uses the bits of C to select bits from A and B: if the bit in C is 1, that bit is selected from A, and if the bit in C is 0, that bit is selected from B.
xor b
and c
xor b

What could you/would you want to use this for?

I think this is an interesting topic and would like to see it continue Smiley

Like Hot_dog, i often use xor to flip bytes. I think i'm pretty unimaginative though when i code.
Logged
ralphdspam
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: 14 May, 2013, 09:10:11
Date Registered: 01 February, 2011, 07:58:40
Location: California, USA
Posts: 841


Topic starter
Total Post Ratings: +36

View Profile
« Reply #8 on: 29 December, 2011, 09:57:40 »
0

Possibly one of the best uses of XOR is in combination with AND, to act as a bitwise multiplexer.

For example, this uses the bits of C to select bits from A and B: if the bit in C is 1, that bit is selected from A, and if the bit in C is 0, that bit is selected from B.
xor b
and c
xor b

What could you/would you want to use this for?
You can use it for a lot of graphics stuff.  ie: masking sprites, grayscale, etc.
I also use it for preserving certain flags in a register while changing others.
Logged

ld a, 0
ld a, a
chickendude
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Female
Last Login: 12 May, 2013, 18:15:03
Date Registered: 06 September, 2008, 11:27:30
Posts: 435

Total Post Ratings: +66

View Profile
« Reply #9 on: 30 December, 2011, 20:09:06 »
0

Ok, i'm not really too interested in grayscale, but i'd like to see how masking sprites might work out.

Here's some basic code to draw a masked byte to the gbuf:

1
2
3
4
5
6
7
;ix=masked byte to draw
;ix+1=byte mask
;hl=location in gbuf to draw to
 ld a,(ix+1)  ;load mask into a
 and (hl)   ;and the mask onto the byte in gbuf
 or (ix)    ;draw the byte
 ld (hl),a  ;update gbuf
So that would become:

1
2
3
4
5
ld a,(ix)
 xor (hl)
 and (ix+1)
 xor (hl)
 ld (hl),a
Which is an extra byte larger. I've always drawn my masks like this:
sprite:
%00011000
%00111100
%01111110
%11111111

mask:
%11100111
%11000011
%10000001
%00000000
...a 1 being a byte to preserve in the gbuf (whereas i suppose with the xor method it'd be inverted).

If you want, i've got some code from a masked sprite routine i wrote a while back for an rpg i was working on that we could try to optimize Cheesy I don't like how i went about it, first rotating/loading the mask onto the gbuf and updating the gbuf, then rotating/loading the sprite onto the gbuf (drawing twice to the gbuf).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
drawPlayerLoop:
   push bc
drawPlayerRow
   ld d,(ix+32)     ;sprite mask
   ld e,(ix+33)
   ld a,$ff         ;a 1 means the gbuf bit will be preserved
   dec b \ inc b \ jr z,skipMaskClip    ;if b=0, no need to rotate
   scf \ rr d \ rr e \ rra \ djnz $-6   ;rotate mask b bits
skipMaskClip:
   and (hl)         ;a = mask to the furthest right
   ld (hl),a        ;store masked gbuf
   dec hl           ;next byte...
   ld a,(hl)
   and e
   ld (hl),a
   dec hl           ;last byte
   ld a,(hl)
   and d
   ld (hl),a
   pop bc           ;recall b=x offset, how many bits to rotate
   inc hl           ;return hl to starting gbuf position
   inc hl
   push bc          ;store bc again
   ld d,(ix)        ;sprite
   ld e,(ix+1)
   xor a            ;empty a to receive flowover from shifting
   cp b
   jp z,skipSpriteClip
   srl d \ rr e \ rra \ djnz $-5   ;rotate sprite
skipSpriteClip:
   or (hl)          ;load sprite into masked gbuf
   ld (hl),a        ;update gbuf
   dec hl           ;next byte, etc.
   ld a,(hl)
   or e
   ld (hl),a
   dec hl
   ld a,(hl)
   or d
   ld (hl),a
   inc ix           ;next sprite row
   inc ix
   ld de,14         ;jump to next row in gbuf, +2 (draw rightmost byte first)
   add hl,de
   pop bc
   dec c
   jp nz, drawPlayerLoop
Logged
Art_of_camelot
The matrix has you.. ಠ_ಠ
Support Staff
LV12 Extreme Poster (Next: 5000)
*
Offline Offline

Last Login: Yesterday at 22:58:04
Date Registered: 30 August, 2008, 04:55:55
Location: Dr. Light's Laboratory
Posts: 4385


Total Post Ratings: +149

View Profile WWW
« Reply #10 on: 02 January, 2012, 17:22:25 »
0

By the way, are you still working on that RPG any? I always thought it had a really nice look to it. =)
« Last Edit: 02 January, 2012, 17:22:45 by Art_of_camelot » Logged

Projects:
TBA! Coming to an 83+ near you!

NerdTests.com says I'm a Cool Nerd King.  Click here to take the Nerd Test, get nerdy images and jokes, and write on the nerd forum!


chickendude
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Female
Last Login: 12 May, 2013, 18:15:03
Date Registered: 06 September, 2008, 11:27:30
Posts: 435

Total Post Ratings: +66

View Profile
« Reply #11 on: 04 January, 2012, 02:12:20 »
0

Well, my laptop busted (or rather, i busted my laptop) but i had made a fairely recent backup of it (the code on yaronet). Right now i'm a bit spread out but especially once i get Monopoly taken care of (a project that's been biting at my feet for maybe 8 years now) it's next on my hitlist. Actually, a lot of the work i've done with the Monopoly text/menu engine will probably be carried over into the RPG. One of the big things i need to do now is work on the story a bit. Thanks, btw Smiley

Does anyone have any suggestions for the next "Command of the Week"?
Logged
Xeda112358
Xombie. I am it.
Coder Of Tomorrow
LV12 Extreme Poster (Next: 5000)
*
Offline Offline

Last Login: Yesterday at 23:25:09
Date Registered: 31 October, 2010, 08:46:36
Location: Land of Little Cubes and Tea, NY
Posts: 3741


Total Post Ratings: +598

View Profile
« Reply #12 on: 08 January, 2012, 18:36:00 »
0

Okay, time for a new command, ay? Well here goes:
CPIR
CPIR can be an extremely useful and powerful command, especially if you are doing a search.
What it does:
It performs cp (hl) then it decrements BC and increments HL. If the byte was a match, the loop stops and PE is the flag state of the parity/overflow flag and the other compare flags are affected normally. If BC reaches 0, the parity flag is PE.

As an example, if you want to find and jump to a certain line in an executing program, you can do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
;Inputs:
;     DE is the line to jump to
;     (965Bh) points to the start of the program
;     (965Fh) points to the end of the program
;Notes:
;     The two addresses above are set accordingly while a BASIC program is running
     push de
     ld de,(965Bh)     ;This is the start of the currently running program
     ld hl,(965Fh)      ;This is the end of the currently running program
     or a
     sbc hl,de           ;HL is now the size of the program
     ex de,hl
     ld b,d
     ld c,e
     pop de
     dec hl
;HL points to the start of the program
;DE is the line to jump to
;BC are the size of the program
;A is the newline token
SearchLoop:
     dec de
     ld a,d \ or e
     jr z,StoLine
     ld a,3Fh
     cpir
     ret nz                  ;this means BC hit 0. In other words, the line does not exist.
;the byte is 3Fh, but we need to make sure that it is not part of a two-byte token
     dec hl \ dec hl
     ld a,(hl)
     inc hl \ inc hl
     cp 7Eh \ jr z,SearchLoop+5
     cp $AA \ jr z,SearchLoop+5
     cp $BB \ jr z,SearchLoop+5
     cp $EF \ jr z,SearchLoop+5
     cp 5Fh \  jr z,SearchLoop
     sub 5Ch \ jr c,SearchLoop
     sub 7 \ jr c,SearchLoop+5
     jr SearchLoop
StoLine:
     dec hl
     ld (965Dh),hl                  ;this loads the address to the BASIC program counter
     ret
It can probably be optimised a little, but I am sure you get the idea Smiley
« Last Edit: 08 January, 2012, 23:19:59 by Xeda112358 » Logged



Grammer Download (2.29.04.12)
Latest update (possibly incomplete)
My pastebin
Spoiler for FileSyst:
FileSyst is an application that provides a folder and filesystem for the TI-83+/84+ calculators. It is designed to be easy to access and use in BASIC, and it can be used to access game files and save data, or to create a command prompt, among other things:

Spoiler for Graphiti:
This is a graph explorer for graph theory. It will require lots of work to finish. Currently you can:
Add/delete vertices
Add edges (direction not shown, but they are directed)
Arrange vertices in a circle (in the future, you will be able to define levels of rings and the number of nodes in each)
Create complete graphs quickly

Plans:
Add adjacency matrix viewer
Deleting edges
Multiple graphs support
Arrows for directed graphs
Planarity testing
Matrix operations
Weighted edges
Chromatic polynomials
Chromatic numbers

Spoiler for Stats:

Samocal             [o---------]
Virtual Processor   [o---------]
EnG                 [oo--------]
Grammer             [ooo-------]
AsmComp             [ooo-------]
Partex              [oooo------]
BatLib              [oooooooo--]
Grammer82           [----------]
Grammer68000        [----------]


Pseudonyms:  Zeda, Xeda, Thunderbolt
Languages:   English, français
Programming: z80 Assmebly
             Grammer
             TI-BASIC (83/84/+/SE, 89/89t/92)
Known For:   -Creator of the Grammer programming language
              (Winning program of zContest2011)
             -BatLib- One of the most feature packed libraries for BASIC programmers available
              with over 100 functions and a simple programming language
             -Learning to program z80 in hexadecimal before using an assembler (no computer was
              available!)
╔═╦╗░╠═╬╣▒║ ║║▓╚═╩╝█


ralphdspam
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: 14 May, 2013, 09:10:11
Date Registered: 01 February, 2011, 07:58:40
Location: California, USA
Posts: 841


Topic starter
Total Post Ratings: +36

View Profile
« Reply #13 on: 14 January, 2012, 02:55:38 »
0

Nice code, but I can't think of any other uses of the command.  Can you think of any hacky type usage?
Logged

ld a, 0
ld a, a
Xeda112358
Xombie. I am it.
Coder Of Tomorrow
LV12 Extreme Poster (Next: 5000)
*
Offline Offline

Last Login: Yesterday at 23:25:09
Date Registered: 31 October, 2010, 08:46:36
Location: Land of Little Cubes and Tea, NY
Posts: 3741


Total Post Ratings: +598

View Profile
« Reply #14 on: 14 January, 2012, 03:10:26 »
0

You can use cpir to determine the length of a string or name. For example, if you want the length of a program name in OP1 with a maximum limit of 9:

1
2
3
4
5
6
7
8
9
     ld bc,9
     ld hl,OP1
     xor a
     cpir
     jr nz,$+3                ;This means the string ended at maximal length
       ccf
     ld a,9 \ sbc a,c
     ld c,a
In fact, you can turn that into a general code where you can find the length of a zero-terminated string pointed to by HL with a maximum size:

1
2
3
4
5
6
7
8
9
     push bc
     xor a
     cpir
     pop hl
     jr nz,$+3                ;This means the string ended at maximal length
       ccf
     sbc hl,bc           ;the c flag was reset by 'xor a', so hl is the length
     ret

There are other ways that I have used it, too, I just need a moment to think of some more uses Big smile I was actually hoping some other coders had neat hacks.

EDIT: Another use for this is when you want to display a string, but only up to x digits.
« Last Edit: 14 January, 2012, 03:13:28 by Xeda112358 » Logged



Grammer Download (2.29.04.12)
Latest update (possibly incomplete)
My pastebin
Spoiler for FileSyst:
FileSyst is an application that provides a folder and filesystem for the TI-83+/84+ calculators. It is designed to be easy to access and use in BASIC, and it can be used to access game files and save data, or to create a command prompt, among other things:

Spoiler for Graphiti:
This is a graph explorer for graph theory. It will require lots of work to finish. Currently you can:
Add/delete vertices
Add edges (direction not shown, but they are directed)
Arrange vertices in a circle (in the future, you will be able to define levels of rings and the number of nodes in each)
Create complete graphs quickly

Plans:
Add adjacency matrix viewer
Deleting edges
Multiple graphs support
Arrows for directed graphs
Planarity testing
Matrix operations
Weighted edges
Chromatic polynomials
Chromatic numbers

Spoiler for Stats:

Samocal             [o---------]
Virtual Processor   [o---------]
EnG                 [oo--------]
Grammer             [ooo-------]
AsmComp             [ooo-------]
Partex              [oooo------]
BatLib              [oooooooo--]
Grammer82           [----------]
Grammer68000        [----------]


Pseudonyms:  Zeda, Xeda, Thunderbolt
Languages:   English, français
Programming: z80 Assmebly
             Grammer
             TI-BASIC (83/84/+/SE, 89/89t/92)
Known For:   -Creator of the Grammer programming language
              (Winning program of zContest2011)
             -BatLib- One of the most feature packed libraries for BASIC programmers available
              with over 100 functions and a simple programming language
             -Learning to program z80 in hexadecimal before using an assembler (no computer was
              available!)
╔═╦╗░╠═╬╣▒║ ║║▓╚═╩╝█


Pages: [1] 2 3 4   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.294 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.