Assembly
[Crash Course]
Hi! I'm Hanneli (@hannelita)
- Computer Engineer
- Programming
- Electronics
- Math <3 <3
- Physics
- Lego
- Meetups
- Animals
- Coffee
- GIFs
- Pokémon
Agenda
- Why Assembly?
- The old days - 6800
- Our first program
- QA
- The 8086
- MIPS
- Discussion points
- Where to go from here
- References
Why Assembly?
- Web Dev is high level programming (usually)
- What does it happen on low level?
- Let's talk about bits, bytes and the world beyond C
- Interesting for CPU design
- It helps on performance tuning
- Learn by curiosity
Why Assembly?
- Logical puzzles
- Communication with hardware
- Deep understanding of computers
- I wish I had learned Assembly in a pleasant way (my experience was very, very unpleasant)
What do computers understand?
111000011000101010101010110000001111000011100001110000011111100000
We need an efficient way to talk to a computer or machine
We need a language.
Assembly
We use this language (Assembly) and some other tool (Assembler) turns the commands into 0s and 1s
Assembler -> gets Assembly code and outputs 0s and 1s.
The challenge
Build a language that represents the data flow into the hardware
The sketch
Please check @tomerg's presentation
Question: Are all CPUs equal? Do they have the same structure?
No. There are some (lots of) differences.
So, the Assembly varies according the CPU.
(Also, the Assembler varies)
Like dialects
Old stuff
Agenda
- Why Assembly?
- The old days - 6800
- Our first program
- QA
- The 8086
- MIPS
- Discussion points
- Where to go from here
- References
Motorola 6800
1975
Back to the sketch
How do I provide data for the CPU?
Registers
(Insert binaries here)
Accumulator A
A
We can read and write data
What is the size of these registers?
They vary according the architecture, manufacturer, model
LDAA #0h
ADDA #1h
A == #1h
How many registers do I have?
It depends on the processor :)
On 6800, besides Accumulator A, we have another similar register. (guess its name)
A
B
Agenda
- Why Assembly?
- The old days - 6800
- Our first program
- QA
- The 8086
- MIPS
- Discussion points
- Where to go from here
- References
Let's write our first program
- Loads #0h on A
- Adds #1h on A
- Loads #1h on B
- Adds A and B
Our first program
- LDAA #0h
- ADDA #1h
- LDAB #1h
- ABA
- Loads #0h on A
- Adds #1h on A
- Loads #1h on B
- Adds A and B
Agenda
- Why Assembly?
- The old days - 6800
- Our first program
- QA
- The 8086
- MIPS
- Discussion points
- Where to go from here
- References
Questions
- What is the initial value of A and B?
- When I perform the ADDA instruction, where does the result go?
- How does the program know which is the next instruction?
- Am I free to manipulate the content of all of the registers?
- WTH is #?
- Can I manipulate hex numbers only?
- Are the numbers signed or unsigned?
- What happens if the result is larger than 8 bits?
What is the initial value of A and B?
We don't know! That is why we need to add zero to them, or loading a value before adding their content.
When I perform the ADDA instruction, where does the result go?
To A. The results of almost all of the operations usually go to A.
So... Is A mutable?
Yes.
So do I loose the initial value of A after ADDA?
Yes.
Ouch.
Yes.
How does the program know which is the next instruction?
There is (for almost all of the CPUs) a special register that points to the next instruction. We call it Program Counter (PC). At the beginning of the program, it points to zero or any other value established by the manufacturer. The first instruction starts there.
So, are the instructions in memory?
For the 6800, yes.
Am I free to manipulate the content of all of the registers?
Not all of them. There are some that only accept memory addresses as parameters. Other do not support any kind of operation, except for transferring memory addresses.
So Am I allowed to load A with a memory Address?
Yes.
WTH is #?
# is about what we call Address mode. # makes explicit that we are loading the number 0h (0 hex) in A.
What does it happen if I remove #? Fail?
Not in terms of syntax. Removing #, the CPU will load the content of the position 0h of the memory.
Still about #
So removing # gives us a different address mode, right?
Yep! We call it direct addressing. With #, we have the immediate addressing.
Can I manipulate hex numbers only?
No. You can manipulate binaries, decimals. But hex is more common.
Are the numbers signed or unsigned?
We have both representations. Usually, for signed numbers, the left bit represents the signal. For example:
10000001 can be 129 or -1
Who does determine if it is signed or unsigned?
The program's context. And it is tricky. Do not mix them up!
What happens if the result is larger than 8 bits?
( ͡° ͜ʖ ͡°)
We have to consider the sign and the overflow
There is a special register that monitors this information
Flag status register
(each CPU has its own)
Flag status
- Carry, Parity, Zero, Sign, Overflow
- Each bit is a flag
- We need to interpret it properly.
ᕕ( ᐛ )ᕗ
How can we load and manipulate numbers larger than 8 bits?
Alternatives
- Buy a new CPU
- 16 bits = 8 bits + 8 bits - slice the operation (it is a lot of work!)
Buy a new CPU
Agenda
- Why Assembly?
- The old days - 6800
- Our first program
- QA
- The 8086
- MIPS
- Discussion points
- Where to go from here
- References
8086
1978
8086: Your new (old) processor
Data bus - 16 bits
More registers
More instructions
8086: Your new (old) processor
Registers
A
B
C
D
(Accumulator)
(Base)
(Counter)
(Data)
Segmented memory model
CS
DS
SS
ES
(Code)
(Data)
(Stack)
(Extra)
Your code
(Code)
(Data)
(Stack)
With this old new processor, we can do a lot:
- Subroutines
- Macros
- Float point arithmetics
- Manage interruptions (external devices)
Wait. How do you know which are the existing commands?
Datasheet - 30 pages
~1980's, people started to dislike this complex instruction set
Agenda
- Why Assembly?
- The old days - 6800
- Our first program
- QA
- The 8086
- MIPS
- Discussion points
- Where to go from here
- References
MIPS
1984
Reduced instruction set
Simplified set of instructions, similar to several processors in the market.
Agenda
- Why Assembly?
- The old days - 6800
- Our first program
- QA
- The 8086
- MIPS
- Discussion points
- Where to go from here
- References
CISC vs RISC
Microprocessor vs Microcontroller
Little Endian vs Big Endian
I wanna do Assembly.
Agenda
- Why Assembly?
- The old days - 6800
- Our first program
- QA
- The 8086
- MIPS
- Discussion points
- Where to go from here
- References
Try ARM. (RISC)
Modern Intel (simplified)
Modern Intel (simplified)
http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf
(2k pages)
(Protip - several concepts are similar to the old 8086 days)
Agenda
- Why Assembly?
- The old days - 6800
- Our first program
- QA
- The 8086
- MIPS
- Discussion points
- Where to go from here
- References
References
- http://www.alldatasheet.com/datasheet-pdf/pdf/82499/MOTOROLA/6800.html
- http://www.ece.cmu.edu/~ece740/f11/lib/exe/fetch.php?media=wiki:8086-datasheet.pdf
- "The Art of Assembly Language" (Book)
Thank you :)
Questions?
hannelita@gmail.com
@hannelita
Assembly - BuildStuff
By Hanneli Tavante (hannelita)
Assembly - BuildStuff
- 4,135