Converting programs into machine code: some pointers please

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
User avatar
Laserbeak43
Posts: 188
Joined: Fri Sep 21, 2007 4:31 pm
Contact:

Converting programs into machine code: some pointers please

Post by Laserbeak43 »

Hi there,
taking the 6502 simulator tutorials, and after ex02 it tells me to convert the program into machine code myself and then compare it to what 6502 simulator outputs, but i'm not sure how to see what it does output.

anywhay, here's the code:

Code: Select all

; Program to add two 16 bit numbers
; The numbers being added are $0194 and $01BA
; Add1601.65s

	.ORG $0200 	; store machine code starting here
	
	LDA #$94	; Store least significant end of number 1 in first
	STA no1+0	; byte labelled no1
	LDA #$01	; Store MSB end of number 1 in next byte
	STA no1+1	; after the one labeled no1
	
	LDA #$BA	; Store littel end of number 2 in first
	STA no2+0	; byte labelled no2
	LDA #$01	; Store MSB of number 2 in the next byte after
	STA no2+1	; the one labeled no2
	
	CLC		; Clear the carry flag
	LDA no1+0	; load LSB of number 1 into accumulator register
	ADC no2+0	; Add with carry the little end of number 2
	STA res+0	; Store the LSB of the result in a byte labelled res
	LDA no1+1	; Load big end of number 1 into accumulator
	ADC no2+1	; Add with carry the big end of number 2
	STA res+1	; Store the MSB of the result into the byte
			; following the one labeled res
			
	BRK		; stop running the program
	
no1:	.DW $0000	; The .DW directive instructs the assembler to
			; 	reserver one word(16 bits)
no2:	.DW $0000	;	of memory, and allows the programmer to
			;	refer to it by using a label
res:	.DW $0000	; This is how you declare variables
i can find the opcodes with the help of this text (thanks B00daw), but other than that, i'm not too sure i can make sense of most of it besides the addresses. well not enough to convert it to machine code.
what do i do? and how do i check it on 6502 simulator?
Noob sticky!!
Please document this part of the NESdevWiki!! XD
YOU NEED A RETROMACHINESHOP!!
User avatar
Disch
Posts: 1848
Joined: Wed Nov 10, 2004 6:47 pm

Post by Disch »

Machine code has 1 byte for the opcode, followed by 0-2 bytes for the operand (exactly how many depends on the addressing mode of the instruction). 2-byte addresses are stored low-byte first.

Variable names and labels all get converted to raw addresses. Since your variable definitions follow your code, code is 40 bytes long, and is .org'd at $0200 --- 'no1' would be $0228, 'no2' would be $022A, and 'res' would be $022C.

That said... here's some machine code as an example. Compare to that opcode list to see how it works:

Code: Select all

A9 94    (LDA #$94)
8D 28 02 (STA no1+0)
A9 01    (LDA #$01)
8D 29 02 (STA no1+1)
A9 BA    (LDA #BA)
8D 2A 02 (STA no2+0)
... etc
of course it's all mashed together in a binary, and not seperated by neat newlines... so it'd really look like:

Code: Select all

A9 94 8D 28 02 A9 01 8D 29 02 A9 BA 8D 2A 02
I should also point out that this code would not work as you'd expect on an NES because $0200 is RAM on the NES -- and you can't assemble directly to RAM.
User avatar
blargg
Posts: 3717
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg »

Best way to learn machine code is to run a disassembler on some code, perhaps something you wrote and assembled. Be sure it prints the raw hex to the left of the disassembly.
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples »

Disch wrote:you can't assemble directly to RAM.
Tell that to anyone who ever programmed an FDS game, a multicart menu, or anything that runs on an Apple II, C=64, or Atari computer. Running code from RAM is as easy as 1-2-3:
  1. Tell the linker to put a code segment in RAM.
  2. In the init code (running in ROM or in the operating system), copy the segment to RAM.
  3. Jump there.
The source code of the "Forbidden Four" multicart's menu demonstrates one way to set this up on the NES with CA65 and LD65.
blargg wrote:Best way to learn machine code is to run a disassembler on some code, perhaps something you wrote and assembled.
Such as the machine-level debugger in Nintendulator or one of the FCE Ultra variants.
User avatar
Laserbeak43
Posts: 188
Joined: Fri Sep 21, 2007 4:31 pm
Contact:

Post by Laserbeak43 »

oh so that's what a disassmebler's for!! thanks guys :)

thanks for those tips Disch, that will surely come in handy
Noob sticky!!
Please document this part of the NESdevWiki!! XD
YOU NEED A RETROMACHINESHOP!!
User avatar
Disch
Posts: 1848
Joined: Wed Nov 10, 2004 6:47 pm

Post by Disch »

tepples wrote:Tell that to anyone who ever programmed an FDS game, a multicart menu, or anything that runs on an Apple II, C=64, or Atari computer. Running code from RAM is as easy as 1-2-3:
Blah. You know what I meant.

If you going to get overly technical and nitpick on me:

- floppy disks are not RAM
- I didn't say you couldn't relocate code to RAM
- I said "NES", not FDS, Atari, Apple, or what have you

To my knowledge, in every example you gave (FDS, multicarts, and probably the others) the code starts on a floppy or some other non-RAM medium and subsequently gets copied to RAM before it's executed. So I was still right.


=P
griever
Posts: 39
Joined: Thu Apr 05, 2007 1:34 pm

Post by griever »

Hm, but code CAN be relocated in RAM NES, right?
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples »

For completeness:
Disch wrote:- I didn't say you couldn't relocate code to RAM
If it wasn't perfectly clear to me that you weren't ruling out relocating code, then it surely wasn't clear to a beginner.

griever: Yes. Dwedit, one of the authors of an NES emulator for the Game Boy Advance, seems to think The Legend of Zelda copies code to RAM and calls it.

But as a beginner, you probably don't need to think too hard about making your programs copy code to RAM until they start using a mapper and a PRG bigger than 32 KiB.
User avatar
blargg
Posts: 3717
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg »

Hm, but code CAN be relocated in RAM NES, right?
I do all my NES development via code in RAM. Even when I turn on my NES it starts running code from RAM (battery RAM, that is).
Post Reply