Author Topic: I need help with creating programs..  (Read 3465 times)

0 Members and 1 Guest are viewing this topic.

Offline TiAddict

  • LV3 Member (Next: 100)
  • ***
  • Posts: 68
  • Rating: +0/-0
    • View Profile
I need help with creating programs..
« on: April 30, 2011, 04:59:23 pm »
Can someone show me the source code (and explain  :))that makes program that has name from Ans?

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: I need help with creating programs..
« Reply #1 on: April 30, 2011, 05:48:24 pm »
Well hello there! Welcome to the site! Here is some code for you to play with :) I have it organized so that there are mnemonics, then the hex code, then the explanation :
Code: [Select]
;=========================================================
;    Mnemonic            Hex       Explanation
;=========================================================
     bcall(_RclAns)     ;EFD74A    DE points to size bytes
     cp 4               ;FE04      To make sure Ans is a string
     ret nz             ;C0        Quits if a is not equal to 4
     ex de,hl           ;EB        Swaps hl and de. Now hl points to the size bytes of Ans
     ld c,(hl)          ;4E        loads the LSB (Least Significant Byte) of the size into c
     inc hl             ;23        hl points to the next size byte
;
;As a note, these next two lines can be replaced with
;ld b,0 in this case (because the name shouldn't be >8 bytes)
;but this is the general way to get the size of a variable.
;
     ld b,(hl)          ;46        now the MSB of the size is stored to b. BC is now equal to the size of the string in Ans
     inc hl             ;23        hl now points to the actual data
;
;At this point, HL points to the string in Ans
;and BC is the size of the string. This will be
;useful when we copy to OP1 :)
;
     inc a              ;3C        Since a is 4, this makes a=5 (the type for a program)
     ld de,OP1          ;117884    DE points to OP1
     ld (de),a          ;12        Stores a (which is 5) to the byte at OP1+0
     inc de             ;13        DE now points to the next byte (OP1+1)
     ldir               ;EDBO      Copies BC number of bytes from HL to DE. In other words, it copies the string in Ans to OP1
     xor a              ;AF        This makes A=0. It is an optimisation trick :)
     ld (de),a          ;12        This copies a (0) to (de) which is at the end of the string in OP1
     bcall(_ChkFindSym) ;EFF142    This searches for a var named in OP1
     ret nc             ;D0        This stops if the var already exists
     ld hl,256          ;210001    This is the size you want the program to be
     bcall(_CreateProg) ;EF3943    This creates the program
     ret                ;C9

Offline TiAddict

  • LV3 Member (Next: 100)
  • ***
  • Posts: 68
  • Rating: +0/-0
    • View Profile
Re: I need help with creating programs..
« Reply #2 on: April 30, 2011, 06:07:01 pm »
Ahhh thanks! :) this really helps!!  but i dont really get why you have to compare 4 to a.
cp 4               ;FE04      To make sure Ans is a string
and i dont really get how DE and HL works. :(
   EX DE,HL
and why do you increase HL? haha im sorry.. i started ASM about a week ago and reading all the "ASM in 28 days" didnt really make me understand everthing.

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: I need help with creating programs..
« Reply #3 on: April 30, 2011, 06:50:27 pm »
I'd recommend putting down ASM in 28 days for a bit and reading Hot_Dog's Z80 ASM for the Absolute Beginner first. The 28 days tutorial is known to be rather difficult to start off with and Hot_Dog wrote his tutorial specifically to prepare people for that more advanced tutorial.

PS: I'll try to offer an explanation for Xeda's code. However, if anyone else disagrees with me, just ignore me in favor of them :)

The CP 4 compares A to 4, which should be true (set the zero flag) if Ans is a string. If the zero flag isn't set, the next line ends the code and returns. Then the code exchanges DE and HL. This is because bcall(_RclAns) sets DE with a pointer to the size bytes of the string. When you swap DE with HL, HL now contains the pointer to the size bytes. This is necessary because the next command requires HL to hold a pointer and loads the data pointed to by HL into C. When the code then increments HL, it changes the pointer in HL to point to the next byte in memory so that it can then be loaded into B.

Hope this helps :)
« Last Edit: April 30, 2011, 06:51:23 pm by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline TiAddict

  • LV3 Member (Next: 100)
  • ***
  • Posts: 68
  • Rating: +0/-0
    • View Profile
Re: I need help with creating programs..
« Reply #4 on: April 30, 2011, 11:45:56 pm »
oh thanks! hah :) and yeah i did read Hot_Dog's z80 ASM for the Absolute Beginner, but stop at chapter 13 because it got kinda complicated. When i first read it was easy, but then it got little confusing and hard to understand later :(




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: I need help with creating programs..
« Reply #5 on: May 01, 2011, 12:14:29 am »
Yeah, Qwerty has it correct :)

Pretty much, _RclAns does several things:
-Stores the type that Ans is in the "a" register
-Stores the address of the vat entry in HL
-Stores the location of the size bytes in DE

If Ans is a real number, the real number is stored in OP1 and if it is a complex number, it is stored to OP1 and OP2 has the imaginary part.

You will also want to know how variables are stored by the OS. The data itself is stored so that the first two bytes tell the size and the data following is the actual variable data. SO for example, if the hex was 0301454699..., then the size is represented by the 0301 part. Since it is little endian (the little end, in) you need to count it as 0103 which is 259. This means there are 259 bytes of code. Also, because it is little endian, I stored the first byte to c and the second to b. That way, BC contains the total size :)

Offline z80man

  • Casio Traitor
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 977
  • Rating: +85/-3
    • View Profile
Re: I need help with creating programs..
« Reply #6 on: May 01, 2011, 02:37:47 am »
oh thanks! hah :) and yeah i did read Hot_Dog's z80 ASM for the Absolute Beginner, but stop at chapter 13 because it got kinda complicated. When i first read it was easy, but then it got little confusing and hard to understand later :(
That's one of the things with z80 asm I've noticed. At first it is really easy to get into, if not the easiest asm language for beginners. But then when you get into the more advanced topics such as the index registers and the other complex instructions you can get lost. One of the hardest things with z80 is writing perfectly optimized  code because there are often many unique solutions you can take to solving a problem.

Back when the Prizm came out I learned Super H asm and at first I was a little lost with the lack of z80 style literals and the addition of bit shifted displacements and a pipeline architecture. But once you understand the organization of the memory and how to properly use displacements and data tables, it becomes really easy to learn the advanced concepts, especially optimizations. For example in z80 if you want to load 0 into a then you would write xor a instead of ld a,0 because it is faster. In Super H you could do either one, but I prefer MOV 0,R0 instead of XOR R0,R0 beacue they are both 1 clock cycle and MOV is easier to read.

List of stuff I need to do before September:
1. Finish the Emulator of the Casio Prizm (in active development)
2. Finish the the SH3 asm IDE/assembler/linker program (in active development)
3. Create a partial Java virtual machine  for the Prizm (not started)
4. Create Axe for the Prizm with an Axe legacy mode (in planning phase)
5. Develop a large set of C and asm libraries for the Prizm (some progress)
6. Create an emulator of the 83+ for the Prizm (not started)
7. Create a well polished game that showcases the ability of the Casio Prizm (not started)

Offline TiAddict

  • LV3 Member (Next: 100)
  • ***
  • Posts: 68
  • Rating: +0/-0
    • View Profile
Re: I need help with creating programs..
« Reply #7 on: May 01, 2011, 07:05:27 pm »
Oh cool! thanks you guys for helping out! :) imma ask more question later :P hahah

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: I need help with creating programs..
« Reply #8 on: May 01, 2011, 08:04:29 pm »
Yay, I know something about z80!

As for the loading 0 into a register in SuperH, I must admit that I'm a fan of the XOR Rn,Rn method. I believe in trying to equalize my distribution of letters in the source code, plus "xor" looks cooler... :P
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ