You are on page 1of 22

MachineandAssemblyLanguage

MachineLanguage

Themachinelanguageforaparticularcomputeristiedto thearchitectureoftheCPU. Forexample:G4Macshaveadifferentmachine languagethanIntelPC's. Wewilllookatthemachinelanguageofasimple, simulatedcomputer.

VonNeumannArchitecture
Memory CPU Control Unit Registers

ALU

SlightlyMoreCompletePicture
Memory CPU Control Unit Registers

ALU

Secondary Storage

VonNeumannArchitecture
Program and Data are both stored in memory. Fetch, Decode, Execute cycle

Memory CPU Control Unit Registers

ALU

MachineLanguageExample

Ourcomputerhas4registersand32memorylocations. Eachinstructionis16bits. Hereisamachinelanguageprogramforoursimulated computer:


1000000100100101 1000000101000101 1010000100000110 1000001000000110 1111111111111111

SampleInstruction
InstructionID Register# MemoryLocation

LOAD contents of memory location into register

100000010 RR MMMMM example: R0 = Mem[3] 100000010 00 00011

MachineLanguage
LOAD contents of memory location into register 100000010 RR MMMMM ex: R0 = Mem[3] 100000010 00 00011 100000100 RR MMMMM ex: Mem[4] = R0 100000100 00 00100 100100010000 RR RR ex: R0 = R1 100100010000 00 01

STORE contents of register into memory location

MOVE contents of one register into another register

MachineLanguage
ADD contents of 2 registers, store result in third. 1010000100 RR RR RR ex: R0 = R1 + R2 1010000100 00 01 10 1010001000 RR RR RR ex: R0 = R1 R2 1010001000 00 01 10 1111111111111111

SUBTRACT contents of 2 registers, store result into third

Halt the program

ReadingMachineLanguage

Inourcase,firstninebitsspecifiestheoperation,last6 (or7)bitsspecifiesthearguments:

1000000100100101LoadMemory5>R1 1000000101000101LoadMemory5>R2 1010000100000110R1+R2>R0 1000001000000110StoreR0>Memory6 1111111111111111

Itisverytedioustoprograminmachinelanguage.

AssemblyLanguage

Assemblyinstructionsarejustshorthandformachine instructions:
MachineLanguage EquivalentAssembly 1000000100100101LOADR15 1000000101000101LOADR25 1010000100000110ADDR0R1R2 1000001000000110SAVER06 1111111111111111HALT

(Forallassemblyinstructionsthatcomputearesult,the firstargumentisthedestination.) VeryeasytowriteanAssemblyLanguage>Machine languagetranslator.

Exercise

Whatwouldbethe assemblyinstructionsto swapthecontentsof registers1&2?

STORE[MEM][REG] LOAD[REG][MEM] MOVE[REG][REG] ADD[REG][REG][REG] SUB[REG][REG][REG] HALT

ExerciseSolution
STORE1R1 MOVER1R2 LOADR21 HALT

SomeMoreInstructions

Wearemissingsomecrucialfunctionality ??

SomeMoreInstructions

Wearemissingsomecrucialfunctionality Loops!

Branch to a location in memory

BRANCH [MEM]

Branch if the ALU result is BZERO [MEM] zero. Branch if the ALU result is BNEG [MEM] negative.

AMoreComplexExample
R0 R1 R2 R3 3 1 Number 2 0 3 4 5 BZERO 4 BRANCH 0 MOVE R2 R3 HALT 0 1 ADD R3 R2 R3 SUB R0 R0 R1

InMatlab

ThesameprograminMatlabwouldbethefollowing: z=0; x=3; whilex~=0 z=z+y; x=x1; y=z; Orthefollowing: y=y*3;

ProblemswithAssembly

Whymightweavoidwritinginassembly?

HighLevelLanguages:Compilationand Interpretation

Typically,wewriteinalanguagethatis(relatively)easy touse. Atranslatorprogramconvertsourinstructionsto machineinstructionsforthetargetcomputer.


InterpreterTranslationisonthefly.(Matlab) CompilerTranslationhappensallatonce.

Advantagesanddisadvantages...

Compilers,Interpreters,Assemblers

BecauseitisnotfuntoprograminAssembly,we havehighlevelprogramminglanguages.

Matlab

Python,C,C++,Java,Fortran,Cobol,Pascal,M,Ada,Lisp,Ruby,Smalltalk, C#,Haskell,Prolog

Compiler/Interpretertranslatesfromthehighlevel languagetomachinelanguage.

ProgramTranslation

z = 0; x=3; whilex~=0 z=z+y; x=x1; y=z;

ADD R3 R2 R3 SUB R0 R0 R1 BZERO 4 BRANCH 0 MOVE R2 R3 HALT

1010000100000110 1010001000000110 0000001000000100 0000000100000000 1001000100001011 1111111111111111

Compiler/Interpreter

Assembler

MiniLab
http://www.davereed.com/book/source.html