Author Topic: Should I learn ASM?  (Read 10486 times)

0 Members and 1 Guest are viewing this topic.

Offline coops

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +3/-0
    • View Profile
Should I learn ASM?
« on: February 23, 2017, 10:11:10 pm »
So...I'd like to get ya'lls take on ASM...

I've always been interested in learning it, but it seems so foreign of a computer language...is it worth the trouble to learn? Should I stick with axe?

What's the ups and downs? Is there much difference between ti 83 & 84 ASM?

For all you ASM gurus!

Offline CVSoft

  • LV3 Member (Next: 100)
  • ***
  • Posts: 65
  • Rating: +5/-0
    • View Profile
    • CVSoft Homepage (work in progress)
Re: Should I learn ASM?
« Reply #1 on: February 23, 2017, 10:15:15 pm »
I'd suggest at least making an effort to learn it. It's well documented, rather straightforward, and very fast; however, it takes patience and practice to learn.

The TI-83 Plus and TI-84 Plus are practically identical if you don't take advantage of features only the TI-84 Plus has. Even assembly with the TI-84 Plus CSE is only slightly different, due to the different display. The TI-84 Plus CE, however, is entirely different.
Current projects:
Sunθ v4.0 (Solar Position Calculator) (on hold)
CBLLight/CBLTherm (TI-Z80 only)
CBLM (68k only)
--KJ6PSG

Offline coops

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +3/-0
    • View Profile
Re: Should I learn ASM?
« Reply #2 on: February 23, 2017, 10:26:28 pm »
Quote
I'd suggest at least making an effort to learn it. It's well documented, rather straightforward, and very fast; however, it takes patience and practice to learn.

Ya, i'd like to take a crack at it...but i dont even know where to start. Are there any good tutorials out there? Starting from the basics and work your way up.

Offline CVSoft

  • LV3 Member (Next: 100)
  • ***
  • Posts: 65
  • Rating: +5/-0
    • View Profile
    • CVSoft Homepage (work in progress)
Re: Should I learn ASM?
« Reply #3 on: February 24, 2017, 07:03:38 pm »
I got started on the TI-82 with the now very dated TI-82 Assembly Corner tutorial, but TI-83 Plus Assembly in 28 Days (which is currently being updated) is probably the best way to go for anything remotely modern.
Current projects:
Sunθ v4.0 (Solar Position Calculator) (on hold)
CBLLight/CBLTherm (TI-Z80 only)
CBLM (68k only)
--KJ6PSG

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Should I learn ASM?
« Reply #4 on: February 24, 2017, 07:51:57 pm »
For me the most amazing part in learning ASM was starting to understand on a rather low level how computers actually work, i personally found that extremely rewarding

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline coops

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +3/-0
    • View Profile
Re: Should I learn ASM?
« Reply #5 on: February 24, 2017, 09:02:58 pm »
Quote
I got started on the TI-82 with the now very dated TI-82 Assembly Corner tutorial, but TI-83 Plus Assembly in 28 Days (which is currently being updated) is probably the best way to go for anything remotely modern.

Ya, I remember reading that the 28 day tutorial was out dated or incorrect...i'm glad it's getting updated. I'll probably start there. is this what ya'll started off with? Where do you go from there?

Quote
For me the most amazing part in learning ASM was starting to understand on a rather low level how computers actually work, i personally found that extremely rewarding

That's one reason why I'm wanting to learn z80. I'd get a better idea of how computers run, and how (old) games work. Plus, there's some projects I'd like to try that I think I could only do in assembly...

Also, I'm following the 28 day tutorial to get started, and it has me using DOS to compile the asm files...there's gotta be a more efficient way to do that. What do ya'll do?
« Last Edit: February 24, 2017, 10:09:48 pm by coops »

Offline harold

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 226
  • Rating: +41/-3
    • View Profile
Re: Should I learn ASM?
« Reply #6 on: February 25, 2017, 06:38:08 am »
When you're done with 28 days you can just start coding anything you like, you'll improve as you do it
Blog about bitmath: bitmath.blogspot.nl
Check the haroldbot thread for the supported commands and syntax.
You can use haroldbot from this website.

Offline SpiroH

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +153/-23
    • View Profile
Re: Should I learn ASM?
« Reply #7 on: February 25, 2017, 10:02:20 am »
So...I'd like to get ya'lls take on ASM...
I've always been interested in learning it, but it seems so foreign of a computer language...is it worth the trouble to learn? Should I stick with axe?
What's the ups and downs? Is there much difference between ti 83 & 84 ASM?
For all you ASM gurus!
Well, Do you already know how girls work? If you do, you can learn ASM next, if not, look around for a higher level approach.
More seriously, ASM is a journey to the bolts-and-nuts on how the processor language was designed, you might or might not like the raw material's view.
Sometimes it looks ugly down there, but for others is just a wonderful discovery. See,  it all depends. I'd say give it a go, oh and BTW, ASM changes with the cpu, hehe. :P

Offline shmibs

  • しらす丼
  • Administrator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2132
  • Rating: +281/-3
  • try to be ok, ok?
    • View Profile
    • shmibbles.me
Re: Should I learn ASM?
« Reply #8 on: March 09, 2017, 02:23:08 pm »
Well, Do you already know how girls work?
Sometimes it looks ugly down there, but for others is just a wonderful discovery. See,  it all depends. I'd say give it a go, oh and BTW, ASM changes with the cpu, hehe. :P

<<<_<<<

Offline coops

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +3/-0
    • View Profile
Re: Should I learn ASM?
« Reply #9 on: March 09, 2017, 08:47:14 pm »
Quote
Quote from: SpiroH on February 25, 2017, 10:02:20 am

    Well, Do you already know how girls work?

Quote from: SpiroH on February 25, 2017, 10:02:20 am

    Sometimes it looks ugly down there, but for others is just a wonderful discovery. See,  it all depends. I'd say give it a go, oh and BTW, ASM changes with the cpu, hehe. :P


<<<_<<<

Haha, yeah...i still have no idea how girls work!

I got to day 3, and when I saw the convoluted way you do multiplication, i thought 'yeah...i'll sharpen my axe skills'...lol. i'm studying for the FE exam, so most of my concentration is on that, and it looks like learning z80 will have to wait till I'm done. It's just so far from what I learned in c++ and java that I can't rely much on that at all.

However, it is fascinating looking at the bare bones mechanics of how a computer works.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Should I learn ASM?
« Reply #10 on: March 13, 2017, 11:25:06 pm »
But the multiplication isn't convoluted! It's exactly how most of us are taught in grade school, except instead of multiplying digits 0~9, it's just multiplying by 0 or 1 which is super trivial. Like:

         01110101
        x10101101
        ---------
         01110101
        000000000
       0111010100
      01110101000
     000000000000
    0111010100000
   00000000000000
  011101010000000

Or removing the multiplies by zero:

         01110101
        x10101101
        ---------
         01110101
       0111010100
      01110101000
    0111010100000
  011101010000000




So suppose bit 3 is set. Then you basically add your top number, shifted left three times. As an example, suppose you wanted to multiply C*E (ignoring the top 8 bits):
Code: [Select]
;C is our "top" number.
;E is our "bottom" number.
;A will be our "accumulator"

    ld a,0

    rrc e    ;this rotates register 'e' right, putting the bottom bit as "carry" [out of the register].
    jr nc,checkbit1   ;nc == not carry. If "carry" out was zero, skip this step.
    add a,c    ;if carry out was 1, then add to our accumulator.
checkbit1:
    sla c    ;finally, shift our "top number" to the left in case we need to add this to the accumulator, too. Then [REPEAT] 7 more times.
    rrc e
    jr nc,checkbit2
    add a,c
checkbit2:
    sla c
    rrc e
    jr nc,checkbit3
    add a,c
checkbit3:
    sla c
    rrc e
    jr nc,checkbit4
    add a,c
checkbit4:
    sla c
    rrc e
    jr nc,checkbit5
    add a,c
checkbit5:
    sla c
    rrc e
    jr nc,checkbit6
    add a,c
checkbit6:
    sla c
    rrc e
    jr nc,checkbit7
    add a,c
checkbit7:
    sla c
    rrc e
    jr nc,all_done
    add a,c
all_done:
    ret
If you can see how that relates to the school book algorithm, then just know that the following does practically the same thing:
Code: [Select]
;C is our "top" number.
;E is our "bottom" number.
;A will be our "accumulator"

    xor a    ;mad hax to set A to zero. Faster, smaller.
    ld b,8
loop:
    rrc e    ;this rotates register 'e' right, putting the bottom bit as "carry" [out of the register].
    jr nc,no_add   ;nc == not carry. If "carry" out was zero, skip this step.
    add a,c    ;if carry out was 1, then add to our accumulator.
no_add:
    sla c    ;finally, shift our "top number" to the left in case we need to add this to the accumulator, too.
    djnz loop  ;aaand repeat, decrementing register B until zero (this is a specialized instruction on the Z80)
    ret

But please don't use that in a real program :P It's terribly inefficient. If you understand how the register pairing works, you can come up with a much better AND more convoluted algorithm:
Spoiler For "Step-by-Step How to derive the 'best' 8-bit Multiplication Algorithm":
Let's start by rearranging the above code. The way we do 'schoolbook' multiplication starts at the least significant digit, but we can just as easily start from the most significant digit. So let's do an example in base 10:

    377
   x613
   ----
=1*3*377+10*1*377+100*6*377
=1(3*377+10(1*377+10(6*377)))

If we want to convert that last line to pseudo-code:
Code: [Select]
0->acc
10*acc+6*377->acc
10*acc+1*377->acc
10*acc+3*377->acc
So if we wanted something like that in assembly:
Code: [Select]
H*E -> A
;H is the "bottom" number that we will no be checking the top digit down to the bottom digit.
;E is the 'Top" number.
;A is the accumulator
;basic algo, after initializing A to zero.
;    multiply A by 2.
;    shift H left by 1
;    if this results in a bit carried out (so a 1 carried out), then add E ('top' number) to A (the accumulator)
;    repeat 7 more times for all bits in H.
    xor a
    ld b,8
loop:
    add a,a
    sla h        ;shifts H left by 1, bringing in a 0 for the low bit. mathematically the same as H*2 -> H
    jr nc,skip_add
    add a,e
skip_add:
    djnz loop
    ret
That's faster, but not optimal! To get the optimal way, lets stray from optimality a little to make 'L' our accumulator. Since we can't directly add another register to L, we'll have to juggle with register A making it slower:
Code: [Select]
    ld l,0
    ld b,8
loop:
    sla l
    sla h
    jr nc,skip_add
    ld a,l \ add a,e \  ld l,a
skip_add:
    djnz loop
    ret
But since we know that 'sla l' will spit out a zero-bit for the first 8 iterations (all of them), we can do 'sla l \ rl h' which is the same size and speed. However, this is the same as just doing doing "add hl,hl" ! This is where you'll have to learn how register pairs work :P
Code: [Select]
    ld l,0
    ld b,8
loop:
    add hl,hl
    jr nc,skip_add
    ld a,l \ add a,e \  ld l,a
skip_add:
    djnz loop
    ret
But wait, there is more! We don't have an "add l,e" instruction, but we do have an "add hl,de" instruction. If we make D==0, then we can change 'ld a,l \ add a,e \  ld l,a' to 'add hl,de'. The problem is, if the lower byte of HL, (so L) overflows, then the upper byte H, our 'bottom' number. We can't have it changing our input value halfway through the algorithm! Thankfully, the changes never propagate into those upper bits. This requires some tedious work to prove, but if you are cool with taking that at face value, then our last piece of the puzzle gives us:
Code: [Select]
    ld d,0
    ld l,d    ;since D=0 already, this sets L to 0, as we want. It's smaller and faster than ld l,0.
    ld b,8
loop:
    add hl,hl
    jr nc,skip_add
    add hl,de
skip_add:
    djnz loop
    ret
Even better, this actually givers us the full 16-bit result instead of just the lower 8 bits :)

Offline coops

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +3/-0
    • View Profile
Re: Should I learn ASM?
« Reply #11 on: March 15, 2017, 08:25:19 am »
Great explanation Xeda! At first it looked like jibberish, but after looking it over with a pencil, it makes pretty good sense...still a roundabout way to do multiplication :P The concept of using an accumulator for each multiplicative step is new to me. The optimization portion makes sense, although it may take some practice for me to recreate it. I'm also guessing division would be the subtraction instead of addition, trunkating any remainder.

Some quick questions, if that's ok (and be patient with my ignorance, lol):

ld b,8 - This must be telling the loop to repeat 8 times to check each bit, so, how does the loop relate to b? I'm thinking about how nested loops would work...

rrc e - you say this rotates register e, is this the same as changing the register input from left to right?

djnz loop - kindof like "goto" loop, with the automatic decrementing of register b?

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Should I learn ASM?
« Reply #12 on: March 15, 2017, 10:32:24 am »
I'm also guessing division would be the subtraction instead of addition, trunkating any remainder.
Division is typically performed exactly like 'schoolbook' long division (until you get to higher precision, then you can use some state-of-the-art algorithms and math magic).

Start with an accumulator and quotient set to 0.
Rotate the one bit of the numerator into the accumulator.
If accumulator>=denominator, then subtract the denominator from the accumulator and shift in a 1 to the quotient, else shift in a zero
Repeat at the "rotate" step.

In code, since we are getting rid of one bit at a time in the numerator and adding 1 bit at a time to the quotient, we can actually recycle the freed up bits in the numerator.
Here is an example of HL/C where C<128, A is the accumulator and HL doubles as the quotient and numerator:
Code: [Select]
HL_div_C:
;Input:
;  HL is the numerator
;  C is the denominator. C<128
;Output:
;  A is the remainder
;  HL is the quotient.
    xor a
    ld b,16
loop:
    add hl,hl   ;this works like shifting HL left by 1. Overflow (thus, the top bit) is left in the carry flag
    rla         ;shift a left, rotating in the c flag as the low bit.
    cp c        ;compare a to c. Basically does A-C and returns the flags. If C>A, then there will be underflow setting the C flag.
    jr c,skip   ;skip the next two bytes if c flag is set (so A<C)
    inc l       ;we know the low bit of HL is 0, so incrementing HL will set that bit.
    sub c
skip:
    djnz loop
    ret
ld b,8 - This must be telling the loop to repeat 8 times to check each bit, so, how does the loop relate to b? I'm thinking about how nested loops would work...
djnz loop - kindof like "goto" loop, with the automatic decrementing of register b?
That is correct and that's why we initially do ld b,8. Keep in mind that djnz * and jr * are intended for small (ish) loops or redirecting to code relatively close by. It can jump backwards up to 126 bytes and forward up to 128 (back 1 byte is simply a slower way of performing rst 38h and I do not suggest it, back 2 bytes creates an infinite loop, back 0 bytes does nothing but waste 7 or 9 clock cycles). Most assemblers will warn you of out-of-bounds jumps.
For longer loops or jumping to code far away, use jp *. djnz * only works with register b and always decrements.

rrc e - you say this rotates register e, is this the same as changing the register input from left to right?
For example, if e=01111011, then rrc e would change it to e=10111101. The bottom bit gets returned in the c flag as well as being returned to the top bit of e. I used rrc * since rotating 8 times would leave it exactly how it was input. I could have used rr * or even sra * or srl *, but I prefer to avoid destroying registers if I can.

Offline coops

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +3/-0
    • View Profile
Re: Should I learn ASM?
« Reply #13 on: March 16, 2017, 09:45:42 pm »
Quote
Division is typically performed exactly like 'schoolbook' long division (until you get to higher precision, then you can use some state-of-the-art algorithms and math magic).

It's all magic to me right now! lol. I can follow the code, but i'll have to do some more reading before I could do anything on my own. This looks like a really steep learning curve...it still says its in novice mode! lol.

How do ya'll assemble these? I currently use windows DOS to compile the files like it says in the 28 day tutorial, but I feel like there's an easier way. Like some sort of IDE

Offline Eeems

  • Mr. Dictator
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6266
  • Rating: +318/-36
  • little oof
    • View Profile
    • Eeems
Re: Should I learn ASM?
« Reply #14 on: March 17, 2017, 12:26:49 pm »
By DOS you mean windows command line? Unless you are still running Windows 98.

There are some IDE's out there, but all I'm aware of have not been updated in a long while and usually make things a little more difficult. I'd suggest just adding build config to your editor of choice (Like Sublime text) and using that.
/e