Anda di halaman 1dari 5

BASICON

ASSEMBLY AND MACHINE LANGUAGE

ASSEMBLY LANGUAGE
- a low level language which employs English-like syntax.
- gives a detailed step-by-step sequence of operations.

• We will be writing programs in the assembly language form from given mathematical
statements and expressions.
• We will be assuming that there is only one register being used, i.e., the accumulator.
• For statements with multiple operations, the following hierarchy will have to be
followed:

() Parentheses
^ Exponentiation
*, / Multiplication, Division
+, - Addition, Subtraction

• We will be using a language of a hypothetical machine with the following commands:

LOAD X Loads the contents of memory X to the accumulator


STORE X Stores the contents of the accumulator into X
ADD X Adds the contents of X to the accumulator
SUB X Subtracts the contents of X from the accumulator
MPY X Multiplies the accumulator by the contents of X
DIV X Divides the accumulator by the contents of X
READ X Reads a value into X
PRINT X Prints the contents of X
STOP Stops or halts the execution of the program

• We will be dealing with straight line programs and programs that involve
looping/branching.

• Looping/branching can be done in two ways:


1. Conditional Branch
- branch if a certain condition is met.
2. Unconditional Branch
- branch to the specified section without any condition.
• The following commands are to be used:

BR X Branches unconditionally to location X


BGE X Branches to location X if the contents of the accumulator is >= 0
BLT X Branches to location X if the contents of the accumulator is < 0
BGT X Branches to location X if the contents of the accumulator is > 0
BLE X Branches to location X if the contents of the accumulator is <= 0
BEQ X Branches to location X if the contents of the accumulator is = 0
BNE X Branches to location X if the contents of the accumulator is <> 0
BASICON
ASSEMBLY AND MACHINE LANGUAGE

Example:

Produce the assembly language of the following program segments:

A. Straight Line Programs

1. B = L + U * E

LOAD U
MPY E
ADD L
STORE B

2. B = R + E * A – K

LOAD E
MUL A
ADD R
SUB K
STORE B

3. B = (A / S * I – C / O) / N

LOAD C
DIV O
STORE TEMP
LOAD A
DIV S
MPY I
SUB TEMP
DIV N
STORE B

4. D = E / N
A=T+E/N–O

LOAD E
DIV N
STORE D
ADD T
SUB O
STORE A
BASICON
ASSEMBLY AND MACHINE LANGUAGE

5. E = A – T
B=R+A–T*S

LOAD A
SUB T
STORE E
LOAD T
MPY S
STORE TEMP
LOAD R
ADD A
SUB TEMP
STORE B

B. Looping/ Branching

1. IF A >= B THEN C = A – B

LOAD A
SUB B
BGE L1
BR L2
L1 LOAD A
SUB B
STORE C
L2 STOP

2. IF A > B THEN D = C + E;
D=B

LOAD A
SUB B
BGT L1
BR L2
L1 LOAD C
ADD E
STORE D
L2 LOAD B
STORE D
STOP
BASICON
ASSEMBLY AND MACHINE LANGUAGE

3. IF X <= Y THEN Z = X * Y
ELSE Z = X / Y

LOAD X
SUB Y
BLE L1
LOAD X
DIV Y
BR L2
L1 LOAD X
MPY Y
L2 STORE Z
STOP

MACHINE LANGUAGE
- the program form that is stored in memory.
- represented as a sequence of digit patterns.

• Each assembly language command is represented by one unique digit pattern.


• A system program called an assembler does the job of converting assembly
language to machine language.
• The machine language follows a particular format, depending on the machine that is
being used.
• For our hypothetical machine, we will be using a 9-bit format: XXXYYYYYY
where XXX is the operation code (op-code) and YYYYYY is the operand address.
• The assembler refers to the symbol table for the address or location of labels and
operands.
• The following are the op-codes of the assembly language commands we used
earlier:

LOAD 000 PRINT 230


STORE 010 BR 300
ADD 020 BGE 310
SUB 030 BLT 320
MPY 040 BGT 330
DIV 050 BLE 340
STOP 100 BEQ 350
READ 210 BNE 360

• When programs are stored in memory, they occupy certain locations with
corresponding addresses. These addresses are to be used in locating certain
operands as well as in locating program segments when branching.
• If we assume that each instruction occupies three memory spaces for this particular
hypothetical computer, then the increment in the actual address will be by three.
BASICON
ASSEMBLY AND MACHINE LANGUAGE

• Using example B-1 in the assembly language generation, and assuming that our
starting address is 000100, the actual address and corresponding machine language
will be as follows:
Actual Address Symbolic Assembly Language Machine Language
Address
000100 LOAD A 000000124
000103 SUB B 030000125
000106 BGE L1 310000112
000109 BR L2 300000121
000112 L1 LOAD A 000000124
000115 SUB B 030000125
000118 STORE C 010000126
000121 L2 STOP 100000100
000124 A DC 1
000125 B DC 1
000126 C DC 1
000127 END

Symbol Table:

Label Address
L1 000112
L2 000121
A 000124
B 000125
C 000126

Note:
The operand address for the command STOP refers to the address of the first
executable command of the program, which, in this case, is in location 000100.

• ASSEMBLER DIRECTIVES

1. DS (Define Storage)
- instructs the assembler to allocate memory space for variables.

Examples:
A DS 1 {one location (space) is set for variable A}
R DS 3 {three consecutive locations are set aside for R}

2. DC (Define Constant)
- allocates a cell and sets its value to a certain constant.

Examples:
TWO DC 2 {sets the value of the variable TWO to 2}
PI DC 3.14 { sets the value of the variable PI to 3.14}

3. END
- notifies the assembler that the end of the program has been reached.

Anda mungkin juga menyukai