#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Assembler question


    I know that this very well may be a stupid question, and I know that it isn't Windows specific, but how does an operating system at its most primitive level, understand assembler? Does it have a compiler or something stored somewhere? If anyone knows a place where I can learn about this, it would be very appreciated.
  2. #2
  3. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,439
    Rep Power
    4539
    Computers understand only binary bits. Numbering systems like hexidecimal and octal, assembly language and higher-level compilers are only around to make binary bits more readable and understandable by humans.

    An assembler like MASM does the same thing a compiler does, it creates a collection of binary bits from some human-friendly language.

    You could start by finding the instruction set for a simple CPU like an old 8008 or Z80 or 6502 chip to get a feel for how the computer actually does things. Newer processors still do the same things and work basically the same way older processors did, just a whole lot faster and with more built-in math and other functions today.

    I had to manually key in a 6-instruction bootstrap program on my first home computer. At that time there was no concept of a BIOS, just a chunk of bare metal waiting for some binary bits to tell it what to do.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    An operating system isn't responsible for actually executing the code, so it doesn't need to understand assembly. As Doug mentioned, assembly isn't actually the most primitive level. An assembler takes assembly and converts it from human-readable code into a series of instructions. Each instructions is some number of bits long (determined by the architecture). Many assembly instructions map into a single machine instruction, but some assembly instructions will be mapped into multiple machine instructions by the assembler. The machine instructions themselves are virtually unreadable to humans because they are nothing more than a series of bits (most commonly represented as numbers).

    So the assembler itself exists at the same level as any other program on your computer.

    The processor hardware is responsible for actually executing those machine instructions. It reads them directly and performs the actions they say to take. The logic for executing the instructions is physically built into the processor hardware. The processor hardware cannot understand assembly.

    You may want to look up information on RISC. It's great for learning the basic concepts.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0
    Thank you, this clears some things up. But -- how is an instruction set defined to a processor? For example if something like the code "10000000" is fed to a processor, which means "NULL," how does it know that this order of digits does this thing? What is a flag, or a register? I know this a lot, but any hep would be greatly appreciated.
  8. #5
  9. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,439
    Rep Power
    4539
    Originally Posted by jmsparkland1
    Thank you, this clears some things up. But -- how is an instruction set defined to a processor? For example if something like the code "10000000" is fed to a processor, which means "NULL," how does it know that this order of digits does this thing? What is a flag, or a register? I know this a lot, but any hep would be greatly appreciated.
    The manufacturer of the processor determines the actual instruction set. For pretty much as long as there have been digital computers the "CPU" has consisted of a few "registers" that hold "words" of data, and some kind of sequencer that acts on the data in registers.

    The first few computers I worked on only added. They had a register, an adder register and an accumulator register that collected the results from the adder. Instructions are executed sequentially from memory (memory, memory addressing and such are for another discussion). The cpu is given a starting address of a program. The contents of the first memory address are loaded into the opcode register where the processor hardware decodes the instruction and acts accordingly. The processor then automatically sequences to the next memory address and repeats the cycle, unless the opcode caused a "jump" to move the program execution to a different spot in memory.

    Typical machine instructions do things like fetch a word from memory and put it in a cpu register, add the contents of two registers, conditionally jump if a register contens meet some test like being zero, postitve, negative, etc.

    This cycle of executing insstructions continues until the cpu reaches a stop instruction or exhausts the memory.

    Modern PC's have a built-in start address that points the processor to the BIOS, which is just a program stored in non-volitale memory.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    The instruction set is defined only by the physical electronic properties of the processor's circuity. Its definition, as far as the processor is concerned, is literally only the order in which the gates and components in the processor are physically wired to one another.

    Of course, since a human cannot look at a schematic with a billion transistors on it and make any sense of it, documentation for humans is written in a high level human language like English.

    As a drastically simplified example, if I have a 32 bit instruction it means the processor starts by applying 32 separate voltages to different input "pins". Those pins are connected to different chains of logic gates, which could eventually connect to 32 output "pins". The "instruction set" is documentation of what the values of those output pins will be given any set of input pin values.

    "NULL" is a high level language construct; in general a processor would have no concept of it, they deal only with numbers.

    A flag is normally a boolean (one bit) value.

    A register is a small piece (32 or 64 bits commonly) of memory on the processor that can be accessed directly by most instructions. Most mathematical operations use the values from two registers as their operands.


    Wikipedia actually has a really good reference for how to encode instructions for the MIPS RISC architecture.
    http://en.wikipedia.org/wiki/MIPS_architecture
    That will give you an indication of why this is pretty much never done by hand anymore except as a learning exercise.

    Each instruction is a 32 bit number (for MIPS). This consists of some bits to specify the format of instruction (which defines its arguments), some bits to specify the type of the instruction (which defines what it does) and then some variable number of arguments (depending on the format).

    As an example, in an I-format instruction the first 6 bits specify the format and type of instruction. If these bits have the value 001001, then the processor performs an addiu instruction. The next 5 bits specify the number of the destination register (0-31). The results of the addition will be saved to that register's memory. The next 5 bits specify the number of a source register (0-31); this will be used as one of the operands in the addition. The remaining 16 bits are treated as an unsigned integer and added to the value taken from the source register. The result is then stored back to the destination register. When that is complete, the instruction is fully executed.

    So, if I give the number 606,076,930 to the processor as an instruction input, it will add 0+2 and store the result in register #1 (register #0 always has a value of 0, which is where the 0 comes from).
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo