Author Topic: Variables, Labels, Loops  (Read 9401 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Variables, Labels, Loops
« on: April 12, 2011, 06:34:30 am »
I have this code (in Mimas):

Code: [Select]
Start:
  db $ef,$40,$45
  ld b,100
  ld a,0
Loop:
  add a,myNum
  djnz Loop
  ld l,a
  db $21,$01,$00
  db $ef,$07,$45
  B_CALL (_GetKey)  ;ef4972
  ret
myNum:
  db 3

I'm trying to make a loop where I add 3 to a 100 times.

Will this ever work? I'm not sure if I can set variables like that or only by (myVar .equ 3).

Thanks.

Offline aeTIos

  • Nonbinary computing specialist
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3915
  • Rating: +184/-32
    • View Profile
    • wank.party
Re: Variables, Labels, Loops
« Reply #1 on: April 12, 2011, 06:49:15 am »
Mimas=cool, huh? :)
You'll get 300-255=a 45 higher than the offset because the registers are 8-bits. I think this will work, you just should try it out.
I'm not a nerd but I pretend:

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Variables, Labels, Loops
« Reply #2 on: April 12, 2011, 07:22:17 am »
aeTIos: That's not the problem, this won't work too, I think:

Code: [Select]
Start:
  db $ef,$40,$45
  ld b,100
  ld a,0
Loop:
  add a,myNum
  djnz Loop
  ld l,a
  db $21,$01,$00
  db $ef,$07,$45
  B_CALL (_GetKey)  ;ef4972
  ret
myNum:
  db 1

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Variables, Labels, Loops
« Reply #3 on: April 12, 2011, 06:24:22 pm »
myNum isn't 1; it's a pointer to a byte that's one. In other words, you're adding something like 40,371 to A every pass.

EDIT: Quick explanation of pointers here if you're confused :)
« Last Edit: April 12, 2011, 06:30:41 pm by Deep Thought »




Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: Variables, Labels, Loops
« Reply #4 on: April 12, 2011, 06:31:44 pm »
You need to do add a,(myNum)

myNum is merely labeling the address at which the data is found. You then have to indirectly load the data from the address.
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Variables, Labels, Loops
« Reply #5 on: April 12, 2011, 06:34:44 pm »
I don't think ADD A,(n) is valid...

You'll have to do LD HL,myNum \ ADD A,(HL).




Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: Variables, Labels, Loops
« Reply #6 on: April 12, 2011, 06:35:36 pm »
Ah yes, you're right. My bad.
There's something about Tuesday...


Pushpins 'n' stuff...


Offline ralphdspam

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 841
  • Rating: +38/-1
  • My name is actually Matt.
    • View Profile
Re: Variables, Labels, Loops
« Reply #7 on: April 12, 2011, 08:34:09 pm »
You can use ld a,(Address) instead of loading the address to HL first. :)
ld a, 0
ld a, a

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Variables, Labels, Loops
« Reply #8 on: April 12, 2011, 09:03:20 pm »
Just a question. Why are you doing in-line assembly in assembly? Why not just type out the bcall? It makes it easier for the non-xeda's to read :D
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline ralphdspam

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 841
  • Rating: +38/-1
  • My name is actually Matt.
    • View Profile
Re: Variables, Labels, Loops
« Reply #9 on: April 12, 2011, 09:13:28 pm »
Also, if you want to do multiplication (I think that is what you want), you can use one of these routines:
http://wikiti.brandonw.net/index.php?title=Z80_Routines:Math:Multiplication
ld a, 0
ld a, a

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Variables, Labels, Loops
« Reply #10 on: April 13, 2011, 04:25:24 am »
Just a question. Why are you doing in-line assembly in assembly? Why not just type out the bcall? It makes it easier for the non-xeda's to read :D

But easier for me to code, sorry.

add a,(mynum) Thanks, I actually though stuff inside parentheses were adresses and without parentheses were values. Is it different for variables??

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Variables, Labels, Loops
« Reply #11 on: April 13, 2011, 03:58:08 pm »
Quote from: ralphdspam
You can use ld a,(Address) instead of loading the address to HL first. :)

It needs to be added to A, though.

Quote from: Scout
add a,(mynum)

See my post above -- ADD A,(n) isn't valid.

Quote from: Scout
Thanks, I actually though stuff inside parentheses were adresses and without parentheses were values. Is it different for variables??

That's exactly it. myNum is a pointer that points to a byte that has a value of 3.

I don't think you understand what "variables" are in ASM. In ASM, a "variable" is just a byte (or two bytes) of data reserved for a purpose. In this case, you use .db to reserve a byte that has a value of 3, and you label it as myNum -- the pointer of that byte.




Ashbad

  • Guest
Re: Variables, Labels, Loops
« Reply #12 on: April 13, 2011, 04:06:02 pm »
This should work:

Code: [Select]
   XOR A
   LD H,0
   LD B,100
Loop
   INC  A
   DJNZ Loop
End
   LD L,A
   RST 28h
   .dw DispHL
   RET

This assumes that since MyVar is one, it can just use an increment sequence instead :)
   

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Variables, Labels, Loops
« Reply #13 on: April 14, 2011, 06:03:46 am »
Quote from: ralphdspam
You can use ld a,(Address) instead of loading the address to HL first. :)

It needs to be added to A, though.

Quote from: Scout
add a,(mynum)

See my post above -- ADD A,(n) isn't valid.

Quote from: Scout
Thanks, I actually though stuff inside parentheses were adresses and without parentheses were values. Is it different for variables??

That's exactly it. myNum is a pointer that points to a byte that has a value of 3.

I don't think you understand what "variables" are in ASM. In ASM, a "variable" is just a byte (or two bytes) of data reserved for a purpose. In this case, you use .db to reserve a byte that has a value of 3, and you label it as myNum -- the pointer of that byte.

That explained it all, thanks a lot.