Intro to Assembly Programming (Background info to program a paintball marker)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Miscue
    Super Moderator

    • Oct 2000
    • 7105

    #1

    Intro to Assembly Programming (Background info to program a paintball marker)

    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
    ubi dubium ibi libertas
    • Apr 2002
    • 922

    #2
    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

    "Automags.org. You'll never find a more wretched hive of scum and villainy."

    Comment

    • Miscue
      Super Moderator

      • Oct 2000
      • 7105

      #3
      Originally posted by Jack_Dubious
      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!

      Comment

      • SlipknotX556
        Registered User
        • Nov 2001
        • 5054

        #4
        Originally posted by Miscue
        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.


        Tiger Stripe Barrel Condoms F/S
        Warped Kustoms - Custom Headgear and Apparel
        Click Here For My AO Feedback
        Click Here For My PBN Feedback

        Comment

        • SlartyBartFast
          The Flying Scotsman
          • Jun 2002
          • 2940

          #5
          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.

          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.

          Real Nerds(TM) Program in Binary.

          Comment

          • Python14
            Norsk
            • Jun 2001
            • 3343

            #6
            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.
            BLOODY MURDER!

            Comment

            • Miscue
              Super Moderator

              • Oct 2000
              • 7105

              #7
              Originally posted by SlartyBartFast
              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.

              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.

              Real Nerds(TM) Program in Binary.
              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.
              Last edited by Miscue; 09-15-2004, 06:07 PM.

              Comment

              • MaChu
                AO's HalfBreed Mix
                • Feb 2003
                • 425

                #8
                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.
                Black 1972' Datsun 240Z(I6 Goodness)

                My Feedback

                Comment

                • WorrNemesis
                  Registered User
                  • Sep 2004
                  • 3

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

                  Originally posted by Miscue
                  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.

                  Originally posted by MaChu
                  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.
                  There are only 10 types of people in this world,
                  Those who understand binary and those who don't.

                  Comment

                  • behemoth
                    SVSTC?
                    • Nov 2002
                    • 7750

                    #10
                    Hey Cue! see what i dug up!! ^^^^^

                    Comment

                    • Miscue
                      Super Moderator

                      • Oct 2000
                      • 7105

                      #11
                      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!):


                      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:


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

                      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.





                      Originally posted by WorrNemesis
                      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.
                      Last edited by Miscue; 09-15-2004, 09:09 PM.

                      Comment

                      • CasingBill
                        The Case Wang
                        • Jan 2003
                        • 1347

                        #12
                        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?
                        My AO Feedback
                        My PBNation Feedback

                        Comment

                        • Miscue
                          Super Moderator

                          • Oct 2000
                          • 7105

                          #13
                          Originally posted by behemoth
                          Hey Cue! see what i dug up!! ^^^^^
                          Ha... I just noticed your sig.

                          Comment

                          • Jack_Dubious
                            ubi dubium ibi libertas
                            • Apr 2002
                            • 922

                            #14
                            Originally posted by CasingBill
                            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


                            JDub

                            "Automags.org. You'll never find a more wretched hive of scum and villainy."

                            Comment

                            • CasingBill
                              The Case Wang
                              • Jan 2003
                              • 1347

                              #15
                              Originally posted by Jack_Dubious
                              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


                              JDub

                              I knew somebody had to get it
                              My AO Feedback
                              My PBNation Feedback

                              Comment

                              Working...