Omnimaga

Calculator Community => TI Calculators => ASM => Topic started by: Munchor on April 12, 2011, 06:34:30 am

Title: Variables, Labels, Loops
Post by: Munchor 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.
Title: Re: Variables, Labels, Loops
Post by: aeTIos 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.
Title: Re: Variables, Labels, Loops
Post by: Munchor 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
Title: Re: Variables, Labels, Loops
Post by: Deep Toaster 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 (http://ourl.ca/9165/180882) if you're confused :)
Title: Re: Variables, Labels, Loops
Post by: ZippyDee 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.
Title: Re: Variables, Labels, Loops
Post by: Deep Toaster 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).
Title: Re: Variables, Labels, Loops
Post by: ZippyDee on April 12, 2011, 06:35:36 pm
Ah yes, you're right. My bad.
Title: Re: Variables, Labels, Loops
Post by: ralphdspam on April 12, 2011, 08:34:09 pm
You can use ld a,(Address) instead of loading the address to HL first. :)
Title: Re: Variables, Labels, Loops
Post by: thepenguin77 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
Title: Re: Variables, Labels, Loops
Post by: ralphdspam 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
Title: Re: Variables, Labels, Loops
Post by: Munchor 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??
Title: Re: Variables, Labels, Loops
Post by: Deep Toaster 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 (http://ourl.ca/10330/197146) -- 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.
Title: Re: Variables, Labels, Loops
Post by: Ashbad 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 :)
   
Title: Re: Variables, Labels, Loops
Post by: Munchor 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 (http://ourl.ca/10330/197146) -- 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.