I have finally gotten an assembler working on my laptop so i have decided to venture out into the world of asm again. And I was going to go ahead and try to create sample programs from what i learn in 28 days, and also post some of the code used in that tutorial with comments on how i think it works. Then someone could possibly explain if i am on the right track or something similar.

Edit:Thanks to bfr and b-flat, my previous question has been solved so i will just go ahead and post how i think the code is working.

The Code:

Code:

.nolist
#include "ti83plus.inc"    ; Standard header stuff
#define progstart $9d95
.list
.org progstart -2
  .db $bb,$6d
  ld a,3                          ;sets a = 3
  ld b,a                          ; now b = 3 also
  add a,a                       ; a + a = 6
  add a,b                       ; a + b = 9
 
  ld d,0                           ;  de = 9
  ld e,a
 
  ld hl,array                    ; hl = memory address of array
  add hl,de                     ;memory address +  index(9) = i don't know
  ld c,(hl)                         ;  c is now equal to the value stored at hl's address
  ld h,0                             ; hl is now the equivalent of c
  ld l,c
     
  push hl                             ; hl is put on the stack
  b_call(_clrlcdfull)         ;screen is cleared   
  pop hl                             ; retrieve hl from the stack
  b_call(_disphl)             ; display the value of hl
  ret                                    ; end of code
 
array:                                                ;array data
  .db 0,1,2,3,4,5,6,7,8,9,10
 
.end
.end


Edit 2: Here is my attempt at 2d arrays. For some reason though it returns 9 instead of 10 like i thought it should.


Code:
.nolist
#include "ti83plus.inc"
#define progstart $9d95
.list
.org progstart - 2
  .db $bb,$6d
  b_call(_clrlcdfull)
 
  ld b,2           ;b = 2
  ld c,4           ;c = 4
  ld hl,array      ;hl = address of array
  ld a,c           ;a = 4
  add a,a          ;a = 8
  add a,a          ;a = 16
  add a,b          ;a = 18
  ld d,0           ;de = 18
  ld e,a
  add hl,de        ;address of hl + 18
 
  ld a,(hl)        ;a = value stored at address hl
  inc hl           ;hl = address + 1
  ld h,(hl)        ;hl = value stored at hl
  ld l,a
  b_call(_disphl)  ; display hl
  ret

array:                             ;array data
  .dw 0,1,2,3,4,5,6
  .dw 7,8,9,10,11,12
  .dw 13,14,15,16,17,18
  .dw 19,20,21,22,23,24
  .dw 25,26,27,28,29,30
 
.end
.end
0+(18/2) = 9. What's puzzling about that?
Where did you get the 0 and the 2, and i guess the 18 comes from what is loaded into de?
lafferjm wrote:
Where did you get the 0 and the 2, and i guess the 18 comes from what is loaded into de?
You're starting at index zero. Because you wrote .dw, not .db, each of the numbers in your table takes two bytes. Therefore, if 0 is at byte 0, 1 is at byte 2, 2 is at byte 4..., and 9 is at byte 18 (and 19, of course). Got it?
Makes sense, and if my next question sounds really stupid sorry. If i used db instead of dw would the result be 10?
lafferjm wrote:
Makes sense, and if my next question sounds really stupid sorry. If i used db instead of dw would the result be 10?
No, the result would be 18. .dw means every number takes 2 bytes, so if .db means every number is one byte, byte 0 contains 0, 1 contains 1, blah blah blah 18 contains 18.
Alright it makes a little more sense. Let me go mess around with the code a bit more and see what kind of values i can get to help me out.

Edit: It seems as though the tutorial in 28 days doesn't even follow its own formula(at least to me) so i am going to attempt to code it myself and see if that may help a little.

Edit 2: Here is my attempt at finding the address of an element in a 2d array. For some reason though it appears to be returning an address and not the number. Also sorry in advanced if i am being frustrating.


Code:
.nolist
#include "ti83plus.inc"
#define progstart $9d95
.list
.org progstart - 2
  .db $bb,$6d
 
  b_call(_clrlcdfull)
  ld b,2
  ld c,2
  ld a,c
  add a,a
  add a,b
  ld d,0
  ld e,a
  ld hl,array
  add hl,de
 
  ld a,(hl)
  inc hl
  ld h,(hl)
  ld l,a
 
  b_call(_disphl)
  ret
 
array:
  .db 0,1,2,3
  .db 4,5,6,7
  .db 8,9,10,11
  .db 12,13,14,15
  .db 16,17,18,19
  .db 20,21,22,23
  .db 24,25,26,27
  .db 28,29,30,31
  .db 32,33,34,35

.end
.end


Edit 3: Well i just decided to load 10 into e and that gave me 5 like it should, now i just have to figure out how to get 10 out of (2,2).

Edit 4: Yes another edit Very Happy I have found out i cant do what i wanted to do in edit 3 until i learn how to do loops so for now i have to do the math manually.
For some reason asm is starting to make a lot more sense to me than it did now. Finding numbers in an array is even really easy and makes tons of sense. Back in the day(two years ago) i couldn't even make heads or tails out of anything. Don't know why, but just to show how much of a noob i was here is a post where i even managed to make the most of the time calm Kerm frustrated http://www.cemetech.net/forum/viewtopic.php?t=1230&postdays=0&postorder=asc&start=0
Wow, no offense, but you were such a n00b back then. Your spelling and grammar has improved significantly, as has your tact in asking questions. I still repeat my suggestion from back then to really understand z80 before diving too far in. I also unlocked the thread you linked to; dunno why it was locked.
I don't know why it was locked either, but it seems almost all of the threads are. Also thanks for the compliment, and i am following your suggestion this time.; just take a look at my first post Cool . Plus this time around i am going in without the thought of making a super great game.
Sorry about double posting but i have gotten to the write back tutorial and i wanted to make sure i absolutely understood what was going on before i continued any further. Here is the code with my comments, someone please correct me if i am wrong.


Code:

.nolist                                           ;beginning of header
#include "ti83plus.inc"
#define progstart $9d95
.list
.org progstart - 2
  .db $bb,$6d                              ;end of standard header
  b_call(_pushrealo1)               ;places op1 on the stack
  ld hl,(counter)                           ;makes hl equal to the value stored at counter's address
  ld de,100                                   ;makes de equal to 100
  add hl,de                                   ;hl = hl+de
  ld (counter),hl                           ;makes the value at location counter equal to hl
  b_call(_disphl)                         ;displays hl
  b_call(_poprealo1)                 ;pops op1 off of stack
  b_call(_chkfindsym)                ;searches for program and de=first byte of data
 
  ld hl,DataStart - $9d95 + 4     ;according to days needed to find start
  add hl,de                                    ;gets offset to where you need to be for writeback
  ex de,hl                                       ;switches de and hl's value
  ld hl,DataStart                            ;datastart is stored to hl
  ld bc,DataEnd - DataStart        ;bc is given a value
  ldir                                                 ;hl and de are incremented while bc is decremented and it copies the data
  ret
 
DataStart:                                    ;labels
counter:  .dw 10000
DataEnd:
yeah, it looks right, but that could be because its a copy/paste of learn z80 in 28 days Razz
I know the code is right and it works i was wondering if my explanation of why it worked by commenting was correct. Wink
Looks right to me. Now, by looking at all the little instructions, can you figure out what the whole program will do when you run it? (That is, if 28 days didn't already tell you.) The crux of assembly is that individual instructions can do almost nothing on their own, but you need to see how they come together to do big things.
Indeed, answer magicdanw. for example, what's with the datastart-$9d95+4 line?
Although this may have nothing to do with the whole purpose of the program, but about this section of code:

Code:
add hl,de
ex de,hl
ld hl,DataStart

Couldn't it be changed to this instead to save space and/or speed?

Code:
add de,hl
ld hl,DataStart

Since you will be swapping hl with de, and then overwriting hl with DataStart, couldn't you add to hl to de instead of the other way around (since addition is commutative, the end product will stay the same)? Or am I missing something since I haven't even gotten past Day 7 of the tutorial?
hl is the two-bit accumulator of sorts, meaning it is the only one that you can add to.
There's no such command as add de,hl. You always have to be adding a=a+* (8-bit) or hl=hl+* (16-bit).
Oops, I meant to say two-byte, or sixteen-bit. Thanks for covering, Kerm Razz
A few notes about comments (some have already been pointed out):

Try to find generalizations of a section of code instead of single instructions.

(my preference, but you don't have to do it) make comments as small (few words) as necessary.

try to find things not obvious and comment on those.

don't be afraid to use blank spaces in the source to seperate blocks of code. it will make it easier to read, and doesn't affect the final negatively at all (in fact, it should allow you to right better code).

Put comments on labels (especially for data/defines) so you and others know what they are used for.

ex: (from your code)

Code:

.nolist                                           ;beginning of header
#include "ti83plus.inc"
#define progstart $9d95
.list

.org progstart - 2
  .db $bb,$6d                              ;end of standard header

  b_call(_pushrealo1)                  ;save name of program so we can find the VAT entry later

  ld hl,(counter)                         ;add 100 to the counter and display it
  ld de,100
  add hl,de
  ld (counter),hl
  b_call(_disphl)

  b_call(_poprealo1)                    ;find this program in the vat and actual storage location
  b_call(_chkfindsym)
 
  ld hl,DataStart - $9d95 + 4     ;find place for writeback and put into DE
  add hl,de
  ex de,hl

  ld hl,DataStart                  ;data we want to writeback
  ld bc,DataEnd - DataStart        ;number of bytes to writeback
  ldir

  ret                       ;quit
 
DataStart:                                    ;start of data to writeback
counter:  .dw 10000                     ;a general counter
DataEnd:                                      ;end of data to writeback
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement