It is currently Wed Aug 15, 2018 10:07 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Sun Sep 22, 2013 12:25 pm 
Offline
User avatar

Joined: Sat Sep 07, 2013 2:59 pm
Posts: 1662
thefox wrote:
DRW wrote:
Code:
  LDX $0104, X ; Loads "length" into X.

This addressing mode doesn't exist, see http://www.obelisk.demon.co.uk/6502/reference.html#LDX

I already feared that this won't work. Alright, last try:
Code:
FillSpecificMemoryWithValue:
; Parameters (put them to the stack in this order):
; 1. Length
; 2. Value

  TSX          ; Loads the current stack pointer.
  LDA $0103, X ; Loads "value" into A.
  LDY $0104, X ; Loads "length" into Y.

Loop:
  STA $1000, Y ; Stores "value" at address $1000 + Y.
  DEY
  BNE Loop     ; If X isn't 0 yet, repeat, otherwise, leave function.

  PLA          ; Removes "value" from stack.
  PLA          ; Removes "length" from stack.

  RTS

Code:
  LDA #$10 ; The parameter "length".
  PSA      ; "Length" is put into the stack.
  LDA #$FF ; The parameter "value".
  PSA      ; "Value" is put into the stack.
  JSR FillSpecificMemoryWithValue


thefox wrote:
As for why $103, consider this example:
[/quote][/quote]
O.k., now I understand.

_________________
Available now: My game "City Trouble".
Website: https://megacatstudios.com/products/city-trouble
Trailer: https://youtu.be/IYXpP59qSxA
Gameplay: https://youtu.be/Eee0yurkIW4
German Retro Gamer article: http://i67.tinypic.com/345o108.jpg


Top
 Profile  
 
PostPosted: Sun Sep 22, 2013 1:03 pm 
Offline
User avatar

Joined: Wed Jun 26, 2013 12:35 pm
Posts: 116
Location: Baltimore
DRW wrote:
thefox wrote:
DRW wrote:
Code:
  LDX $0104, X ; Loads "length" into X.

This addressing mode doesn't exist, see http://www.obelisk.demon.co.uk/6502/reference.html#LDX

I already feared that this won't work. Alright, last try:
Code:
FillSpecificMemoryWithValue:
; Parameters (put them to the stack in this order):
; 1. Length
; 2. Value

  TSX          ; Loads the current stack pointer.
  LDA $0103, X ; Loads "value" into A.
  LDY $0104, X ; Loads "length" into Y.

Loop:
  STA $1000, Y ; Stores "value" at address $1000 + Y.
  DEY
  BNE Loop     ; If X isn't 0 yet, repeat, otherwise, leave function.

  PLA          ; Removes "value" from stack.
  PLA          ; Removes "length" from stack.

  RTS

Code:
  LDA #$10 ; The parameter "length".
  PSA      ; "Length" is put into the stack.
  LDA #$FF ; The parameter "value".
  PSA      ; "Value" is put into the stack.
  JSR FillSpecificMemoryWithValue


thefox wrote:
As for why $103, consider this example:
[/quote]
O.k., now I understand.[/quote]

This will not work. Because you are pulling the return address off the stack, leaving the parameters you pushed to be treated as the return address, the program will crash. If you reserve X as a data stack pointer as I do, you can avoid this specific issue while still keeping the same simple and easy methodology.

Code:
fillstuff:   ; value length --
  tay
  lda 0,x
-
  sta $1000,y
  dey
  bne -
  inx
  drop
  rts

caller:
  dup
  lda #0
  dup
  lda #length
  jsr fillstuff
 


_________________
sonder


Top
 Profile  
 
PostPosted: Sun Sep 22, 2013 1:06 pm 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10702
Location: Rio de Janeiro - Brazil
You can't really remove the parameters from the stack from inside the subroutine though, because the return address is the last thing on the stack. If you take the return address off the stack, the RTS command that follows will crash the program (it will "return" to an invalid location). If it as that easy, nothing would stop you from using PLA right at the beginning to read your parameters instead of using LDA $0103, X, right?

the correct thing to do here would be to pop the parameters out of the stack after the subroutine has returned (i.e. after the JSR), which is not as nice and might trash any return values you have in A. If you really really want to clean up the stack from inside the subroutine, you need to move the return address to the correct place. This is why not many people like to use the stack for passing parameters in 6502 assembly, it's not very efficient.


Last edited by tokumaru on Sun Sep 22, 2013 1:08 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Sun Sep 22, 2013 1:07 pm 
Offline
Formerly 65024U

Joined: Sat Mar 27, 2010 12:57 pm
Posts: 2262
Scratchpad RAM?


Top
 Profile  
 
PostPosted: Sun Sep 22, 2013 1:19 pm 
Offline
User avatar

Joined: Sun Jan 02, 2011 11:50 am
Posts: 522
tokumaru wrote:
You can't really remove the parameters from the stack from inside the subroutine though, because the return address is the last thing on the stack. If you take the return address off the stack, the RTS command that follows will crash the program (it will "return" to an invalid location). If it as that easy, nothing would stop you from using PLA right at the beginning to read your parameters...


That is what I used to do before I abandoned the idea of using the stack for parameters. I had a macro that manually pushed the return address, pushed the parameters, then did a JMP to the subroutine. Not too horrible, but not great.


Top
 Profile  
 
PostPosted: Sun Sep 22, 2013 3:07 pm 
Offline
User avatar

Joined: Sat Sep 07, 2013 2:59 pm
Posts: 1662
O.k., I guess I'll ignore the stack for now and use global variables or the registers directly. Thanks for your answers.

_________________
Available now: My game "City Trouble".
Website: https://megacatstudios.com/products/city-trouble
Trailer: https://youtu.be/IYXpP59qSxA
Gameplay: https://youtu.be/Eee0yurkIW4
German Retro Gamer article: http://i67.tinypic.com/345o108.jpg


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 6 guests


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group