PDA

View Full Version : Intro to Assembly Programming (Background info to program a paintball marker)



Miscue
09-15-2004, 03:18 PM
Since some of you were curious...

CPUs and microcontrollers have an instruction set - which is basically an internal set of commands (instructions) that it understands and can execute. This could be like adding instructions, subtracting, storing or reading from memory, branching, and more complicated instructions.

Assembly is like a human representation of machine instructions. For example, "ADD $1, $2" might directly translate to 1100000000010010 (now I'm just making this completely up, this is a fake 16-bit instruction.)

There is typically a one-to-one correlation between a regular assembly instruction, and a machine instruction. There are "macro" instructions that basically execute multiple assembly instructions - but that's kind of a software based feature in assemblers (software used to produce assembly). Before assemblers, people had to use punchcards (where 1's and 0's are represented by a hole or no hole) and what not to feed their programs into a computer. In a way, a punch card and assembly are very similar - different ways of representing the same thing.

If you look at a high-level language instruction, like say: i = 20+5;
It might look like one line of code... one C language instruction - but really it takes multiple machine instructions to execute this one line - because the processor does not natively understand what that statement means. Compilers are what translate high languages to machine language.

High-level languages do not require you to really understand how the processor works - the CPU kinda becomes an abstraction. Heck, with Java - it uses a "Virtual Machine," which is like a processor that does not physically exist. I suppose you could make an actual Java machine (Never heard of one) kinda like how there are lisp and prolog machines.

I'll continue on later, if I haven't bored ya'll already.

Jack_Dubious
09-15-2004, 04:18 PM
message db "NERD ALERT!",0dh,0ah,0

main proc
mov ax,@data
mov ds,ax

mov ah,9
mov dx,offset message
int 21h

mov ax,4C00h
int 21h
main endp
end main

Miscue
09-15-2004, 04:41 PM
message db "NERD ALERT!",0dh,0ah,0

main proc
mov ax,@data
mov ds,ax

mov ah,9
mov dx,offset message
int 21h

mov ax,4C00h
int 21h
main endp
end main

Ha!

SlipknotX556
09-15-2004, 04:47 PM
Since some of you were curious...

CPUs and microcontrollers have an instruction set - which is basically an internal set of commands (instructions) that it understands and can execute. This could be like adding instructions, subtracting, storing or reading from memory, branching, and more complicated instructions.

Assembly is like a human representation of machine instructions. For example, "ADD $1, $2" might directly translate to 1100000000010010 (now I'm just making this completely up, this is a fake 16-bit instruction.)

There is typically a one-to-one correlation between a regular assembly instruction, and a machine instruction. There are "macro" instructions that basically execute multiple assembly instructions - but that's kind of a software based feature in assemblers (software used to produce assembly). Before assemblers, people had to use punchcards (where 1's and 0's are represented by a hole or no hole) and what not to feed their programs into a computer. In a way, a punch card and assembly are very similar - different ways of representing the same thing.

If you look at a high-level language instruction, like say: i = 20+5;
It might look like one line of code... one C language instruction - but really it takes multiple machine instructions to execute this one line - because the processor does not natively understand what that statement means. Compilers are what translate high languages to machine language.

High-level languages do not require you to really understand how the processor works - the CPU kinda becomes an abstraction. Heck, with Java - it uses a "Virtual Machine," which is like a processor that does not physically exist. I suppose you could make an actual Java machine (Never heard of one) kinda like how there are lisp and prolog machines.

I'll continue on later, if I haven't bored ya'll already.


:confused: :confused: :confused:

SlartyBartFast
09-15-2004, 04:51 PM
Hee Hee. I remember Assembly. Programmed TR(a)S(h)80's with Assembly language back in high school.

How many hundreds and hundreds of lines of code did it take me to create a "game" that had a block moving on the top of the screen and a 'gun' you moved and fired at the bottom. Timed and scored too. :cool:

Why on Earth would you want to do that to yourself now that so many cool tools and efficient compilers exist? Plus the dozens and dozens of routines and libraries that save you months of designing the algorithms and the gruelling debugging.

Oh I know! Becasue every 1 billionth of a second you can shave off monitoring the trigger switch is soooo important in paintball. :rofl: :rofl:

Real Nerds(TM) Program in Binary. :bounce:

Python14
09-15-2004, 05:00 PM
I swear if I see another person in one of those T-shirts that says "There are 10 types of people on earth. Those that understand binary and those that don't" ...I'm gonna pistol whip them.

Miscue
09-15-2004, 06:06 PM
Hee Hee. I remember Assembly. Programmed TR(a)S(h)80's with Assembly language back in high school.

How many hundreds and hundreds of lines of code did it take me to create a "game" that had a block moving on the top of the screen and a 'gun' you moved and fired at the bottom. Timed and scored too. :cool:

Why on Earth would you want to do that to yourself now that so many cool tools and efficient compilers exist? Plus the dozens and dozens of routines and libraries that save you months of designing the algorithms and the gruelling debugging.

Oh I know! Becasue every 1 billionth of a second you can shave off monitoring the trigger switch is soooo important in paintball. :rofl: :rofl:

Real Nerds(TM) Program in Binary. :bounce:

Excellent point!

Months? Nah, you can write basic code for a paintball marker in a couple hours... we're not making Doom3 here. This mentioning of libraries, etc... is kinda carry-over from regular computers - and is a good point to bring up... when talking about regular computers... but is odd to bring up with these little baby microcontrollers in regards to shooting a paintball marker. What libraries are out there for making paintball software? #include paintball.h !!! Efficient compilers for these particular chips... really?

Most if not all of the programmable microcontrollers have compilers available. The problem with these kinds of compilers is they make the code much larger - double, quadruple, or worse. If you can get all of your features within the space limitations - then that's great. These chips commonly have anywhere between 0.5k-64k of program space, depending on what you're using - bloated code can become an issue. If you're making your own board - you can pick whatever chip you want. Otherwise, you're limited to what's out there for a particular marker... or loader depending on what you're working on... and they aren't all 64k chips.

You are also at the mercy of the compiler writer... and I'm not 100% confident in compilers written for a $1 chip. You could conceivably run into problems that cannot be solved because of how it generates code... then again it could work fine, it depends on what you're doing.

Personally, I can write assembly quickly - it's no effort, and I would not benefit much time-wise by using a compiler. With the particular Atmel I worked on for the EMag, the stuff in 4.x would be impossible to fit if a compiler was used.

Compilers = easier and faster to write programs, at the cost of much less efficient code (and this really could be an issue since these microcontrollers aren't necessarily lightening fast), and bloated code that can raise problems if available program space is confining.

Assembly = Clever programming can produce compact, efficient code. More difficult than a compiled language. More difficult to debug. Takes longer to write since the instructions are not nearly as powerful. Tight control of register and memory usage. A feature rich program that uses most of the programming space, cannot fit if a compiler is used to make the same thing.

It's good to learn some assembly even if you don't use it - you can write better high-level code when you have an idea of what the compiler would generate by doing it a certain way.

Whether to use a compiler, or assembler - boils down to what you want to accomplish, and what approach you choose to use. Basically, pick the appropriate tool for a given job.

MaChu
09-15-2004, 07:40 PM
message db "NERD ALERT!",0dh,0ah,0
main proc
mov ax,@data
mov ds,ax

mov ah,9
mov dx,offset message
int 21h

mov ax,4C00h
int 21h
main endp
end main

What language is that? Sorry, not uber nerd, more like nerd in training, Im trying to learn basic C++ and Java in high school.

WorrNemesis
09-15-2004, 07:52 PM
Hah! A subject I'm finally interested in...


If you look at a high-level language instruction, like say: i = 20+5;

Actually, i = 20+5; would probably be 1 line, as long as the compiler is decent since they usually add constants together before making the machine code (sorry for my lack technical terms since I mainly self-taught assembly). It would probably become mov i,25. Anyway, you probably meant something like num3 = num1+num2; in which would be:
mov eax,num1
add eax,num2
mov num3,eax

...not that anybody is interested in this.


What language is that? Sorry, not uber nerd, more like nerd in training, Im trying to learn basic C++ and Java in high school.

That would be assembly, I'm not sure which one would uses 'proc' after name (I don't use the proc macro myself anyway), but I know it's not FASM.

Miscue is completely correct, high level languages allow you to write code quicker, but assembly's control over the CPU can be highly addicting. This control allows for more efficiency, which is a huge advantage for some things, such as 3d rendering.

Now for my question, what is it like writing assembly for a paintball marker (I've never done anything beyond my personal computer)? I don't plan on programming a marker of my own, since I don't own that have any electronic parts, but I am just curious.

behemoth
09-15-2004, 07:55 PM
Hey Cue! see what i dug up!! ^^^^^ ;) :p :D

Miscue
09-15-2004, 08:51 PM
Haha, you're right.

num3 = num1+num2 was what I intended.

What's it like coding for a paintball marker? Well, the instruction sets for programmable microchips are much simpler than full-blown CPUs like x86 or similar. They are very easy, and very intuitive. The Atmel is a RISC-based, load store chip. There's lots of general purpose registers - no surprise. To me, it's a fun ASM if there is such a thing. You don't use interrupts for I/O - I/O is very straight forward using instructions that read from or output to the pins.

You can write a rudimentary semi-auto mode with only two instructions, if I remember... basically set the state of the solenoid to the same state as the trigger button. The solenoid doesn't shut off though, until you release the trigger - so you have to add a timer to shut it off so it won't melt something. Timers to limit ROF... etc... timers, timers, and more timers. Stuff has to happen in sequence, at a certain time... each cycle of the marker.

It was enjoyable to work on it, actually... not the coding, but getting a paintball gun to do what you want it to. You gotta bake the cake to eat it. It's kinda cool to be able to shoot someone repeatedly, with something you made. "I can't wait to bunker someone with this!" Hehehe.

I had this done like the day after I got my programmer (I just had to do it!):
http://www.automags.org/~Miscue/fa.wmv

Few days later: http://www.automags.org/~Miscue/sidescroll.wmv (The paper behind it was what I sketched out to work out the bitmaps for the fonts)

Few days later:
http://www.automags.org/~Miscue/shotbuffer.wmv

So... you can really go from zero microcontroller experience to AGD 3.0 in like a week. :p

I have a joke version somewhere... where the gun stops firing after 50 shots... I'll give that marker to a good friend when we 1v1 !!!

Oh, BTW... it was x86 assembly that he posted.






Hah! A subject I'm finally interested in...



Actually, i = 20+5; would probably be 1 line, as long as the compiler is decent since they usually add constants together before making the machine code (sorry for my lack technical terms since I mainly self-taught assembly). It would probably become mov i,25. Anyway, you probably meant something like num3 = num1+num2; in which would be:
mov eax,num1
add eax,num2
mov num3,eax

...not that anybody is interested in this.



That would be assembly, I'm not sure which one would uses 'proc' after name (I don't use the proc macro myself anyway), but I know it's not FASM.

Miscue is completely correct, high level languages allow you to write code quicker, but assembly's control over the CPU can be highly addicting. This control allows for more efficiency, which is a huge advantage for some things, such as 3d rendering.

Now for my question, what is it like writing assembly for a paintball marker (I've never done anything beyond my personal computer)? I don't plan on programming a marker of my own, since I don't own that have any electronic parts, but I am just curious.

CasingBill
09-15-2004, 09:02 PM
Can I type something like

10 casingbill
20 run 10


will it run "casingbill" continually across the screen????????






I know I totally screwed up the joke, but does anybody get it?

Miscue
09-15-2004, 09:23 PM
Hey Cue! see what i dug up!! ^^^^^ ;) :p :D

Ha... I just noticed your sig. :D

Jack_Dubious
09-15-2004, 11:36 PM
Can I type something like

10 casingbill
20 run 10



That reminds me of a sight gag i saw on Futurama.....there was a sign in a church for robots that read:

10 SIN
20 GOTO HELL

:D
JDub

CasingBill
09-16-2004, 07:07 AM
That reminds me of a sight gag i saw on Futurama.....there was a sign in a church for robots that read:

10 SIN
20 GOTO HELL

:D
JDub


I knew somebody had to get it :D

Fixion
09-16-2004, 07:12 AM
.... and that is why you pic a microcontroller, for which a C compiler exists, and if you really really like ASM, you can still write sections of your program in it. Understand kids? :D

Besides, how are you going to port linux to the emag if you can't comile it for the processor? Better yet, pong?

sig11
09-16-2004, 07:46 AM
.... and that is why you pic a microcontroller, for which a C compiler exists, and if you really really like ASM, you can still write sections of your program in it. Understand kids? :D

The problem is that the emag's microcontroller only has 2k flash, 128bytes of SRAM, and 128bytes EEPROM. Like Miscue already said... you won't get very many features into that if you use a high level language.


The Atmel ASM suprises me with how easy it is. :) I was staring at some code I was given with the programmer and thinking "Why the hell would someone use decimal numbers in ASM?!" Finally after looking at the datasheet (forgot to print the key : ) for the hundreth time I realized that they were specifying certain bits in a register by its decimal index. So cool. In any other assembly language I've used before you had to use masks to set certain bits. Amazing time and mind saver. :)
So nice to do 'sti PORTB, 6' instead of:
'ldaa portb
ldab $20
orab
stab'

I probably butchered that since its been so long since I've written any 68HC11 code.... but you get the idea. :)

Lee

Miscue
09-16-2004, 09:35 AM
.... and that is why you pic a microcontroller, for which a C compiler exists, and if you really really like ASM, you can still write sections of your program in it. Understand kids? :D

Besides, how are you going to port linux to the emag if you can't comile it for the processor? Better yet, pong?

There's no point in doing that. If 90% of your stuff is bloated, might as well do it 100%. If you're programming a PC game, and use inline ASM for parts that need to be very fast - this makes a lot of sense. A goal for ASM usage on a microcontroller is smaller size, when you have space limitations - you gotta do the whole thing in ASM for significant space savings.

Mango
09-16-2004, 09:52 AM
....

Fixion
09-16-2004, 12:31 PM
There's no point in doing that. If 90% of your stuff is bloated, might as well do it 100%. If you're programming a PC game, and use inline ASM for parts that need to be very fast - this makes a lot of sense. A goal for ASM usage on a microcontroller is smaller size, when you have space limitations - you gotta do the whole thing in ASM for significant space savings.

I understand both the advantages and disadvantages of ASM. I was joking ;)
...and no, I didn't take the time to read Miscues second post, actually, I barely read the first few, I had to go to class. :(

I've never seen the emag code, so I'm not going to assume anything. But I did not know what microcontroller the emag uses, so I assumed that its slightly better, faster, stronger (like the song ;)), had more memory, etc. than it actually is. If I knew its specs, I would have agreed that ASM is the only way. But I guess if you want to save money on parts and time on simplicity, thats the way to go.

So, when can I play Duke Nukem Forever on an emag running *nix :p.

datapimp69
09-17-2004, 09:49 AM
hye, whats a stack. and why to i have to pop it?
;)

sig11
09-17-2004, 10:13 AM
hye, whats a stack. and why to i have to pop it?
;)

In my Theory of Computation class we always tried to get the prof off on some wild tangent... Got to be where he would say "Stack push" and start talking about whatever we pushed him to. Though he never popped off the stack when he'd go back to teaching. No wonder that class was so hard. Stack overflow. ;)

Lee

mikey101
09-18-2004, 12:39 AM
public class Stack
{
public static final int SIZE = 2;
private Object [] stck = new Object[SIZE];

public void push(Object x)
{
int i;
int y;
for(i = 0; i != null; i++)
{ y = (i + 1);
}
stck[y] = x;
}
}

- index out of bounds exeception??? OHNOES!!
Java may not be as efficient as asm, but it's still cool with you guys right? :tard: