Intro Asembly Languages
Intro Asembly Languages
Introduction
• Consider the next micro-program for JASPer
MAR<-[IR(operand)]
MDR<-[M[MAR]]
ALUy<-[MDR]
ALUx<-[B]
ALUr=[ALUx]-[ALUy]
• This micro-program compares two numbers, one of these is stored in
the main memory and the other is in the B register. If equals, then the
Z flag will be on.
Introduction
• By grouping sets of micro-instructions together we form higher-level
instructions, known as assembly language instructions
• Assembly language instructions constitute the instruction set of a processor
with an ISA model
• The size of the instruction set depends on the number of bits available for
the opcode field
• For JASPer processor the width of an opcode is eight bits, which means
that we can define a maximum of 256 different instructions in the
instruction set
• In practice, it will not be necessary an instruction set as large as we can
actually write quite complex programs with very few different assembly
language instructions
Introduction
• Writing programs using micro-instruction is a very tough task. This is
the reason why assembly languages exist.
Introduction
• With an assembly language each instruction stored in memory (machine
instruction) has its own mnemonic. In JASPer, the mnemonic for the above
instruction is CMP. To be clear,
Opcode 83
* addr 00 to FF
Mnemonic "CMP addr,B"
Description "Compare a direct oper. with B reg."
MAR<-[IR(operand)]
MDR<-[M[MAR]]
ALUy<-[MDR]
ALUx<-[B]
ALUr=[ALUx]-[ALUy]
Introduction
• Instruction stored in the main memory to execute this micro-program
has opcode 83. See the file instruct.mco.
• When used in a program, any instruction will be coded with the
opcode together with any operands, or parameters to the instruction.
This code is a machine instruction
• For example, if we wanted to write the value 5 into the A register, we
could use an instruction like MOVE #$5,A, which in JASPer, it is stored
as 9005
Introduction
Exercise. In JASPer, find mnmonics for instructions with opcodes 0x25,
0x30 and 0xEF, respectively
Exercise. Find opcodes for the next program writing in assembly
MOVE #$05,A * Transfer the first data value to the A register
MOVE #$03,B * Transfer the second data value to the B register
ADD B,A * Add them, storing the result in the A register
HALT * Stop the program
Put instructions codes in front of each line and save the program as
sum.jas. Then, load the program in JASPer and execute it by using Fetch
Cycle and Execute Cycle buttons. Again, run the program by using the
trace button. Finally, run the program by pressing the Go button
Quick Glance at Assembly Syntax
machine code
directive
label
ORG 00
9005 main MOVE #$05,A * Transfer the first data value to the A register
9103 MOVE #$03,B * Transfer the second data value to the B register
0600 ADD B,A * Add them, storing the result in the A register
F000 HALT * Stop the program
mnemonic
comment
Quick Glance at Assembly Syntax
• Programs in assembly are saved as text files with extension .s. As an
example, the program
MOVE #$05,A * Transfer the first data value to the A register
MOVE #$03,B * Transfer the second data value to the B register
ADD B,A * Add them, storing the result in the A register
HALT * Stop the program
is stored as “sum.txt”
• A special program, called the assembler, takes this file to generate the
machine code, the list of machine instructions. This is the information
that will be stored in the main memory when the program is loaded.
Quick Glance at Assembly Syntax
Quick Glance at Assembly Syntax
• Assembly language syntax
directive
label instruction *comment
Quick Glance at Assembly Syntax
• Labels
Labels are used as references to address locations.
Labels can also be used to reference data addresses. For
example, you can put a label for a lookup table inside
the program. The label name should consist of
alphabets, digits, _ and $.
Quick Glance at Assembly Syntax
main
MOVE #$2e,B * move a '.' into B
JSR putchar * jump to sub-routine putchar
MOVE ISR,A * get the ISR
CMP #$00,A * check if a key has been pressed
BEQ main * if it hasn't then goto main
HALT * finished
putchar …
Quick Glance at Assembly Syntax
• Assembler directives
Assembler directives are instructions that direct the assembler
to do something.
Quick Glance at Assembly Syntax
Example. Copy the next assembly code in a text file.
Save it as sum.txt in jasp folder
ORG 0
MOVE num1,A
MOVE num2,B
ADD B,A
HALT
ORG $10
num1 DC.W $12D5
num2 DC.W $073A
Quick Glance at Assembly Syntax
Open the Command Prompt in folder jasp. Run the
command
jasm -a sum.txt -o sum.jas
Load the file in JASPer, view the program in the
memory and run the program.
Finally, create a comment for each line in the above
program to try to explain what does the program do.
Programming Concepts
Instruction in assembly language are of three types
• Memory access instructions (read memory, write
memory)
• Data processing (arithmetic operations like
“add”/“subtract,” logic operations like “AND”/“OR”)
• Program flow control instructions (branches,
conditional branches, function calls)
Programming Concepts
Memory access instructions
JASPer has instructions which interact with main
memory. These are some of them
MOVE addr,A,
MOVE (addr),A,
MOVE A,addr
Programming Concepts
Data processing
SHL A
SUB (addr),A
ADD #data,A
Programming Concepts
Program flow control instructions
CMP #data,A
BEQ
JSR
Programming Concepts. Sequences
A sequence is simply one instruction followed by
another.
MOVE #$34,A * store the decimal value 34 in A
MOVE #$12,B * store the decimal value 12 in B
ADD B,A * add A and B, storing the result in A
Programming Concepts. Selections
Selections are more complicated than sequences, in
fact there are a few ways to write a selection construct.
These are if construct, if-then-else construct, switch, …