Programming basic computer
-
Upload
martial-kouadio -
Category
Technology
-
view
285 -
download
2
Transcript of Programming basic computer
Programming Basic Computer
• The list of instructions or statements that directs the computer hardware to perform a specific task is called a program.
• There are many programming languages but, the computer basically understands programs that are given in binary (0s, 1s) form. This implies that all other programs should be translated to binary before they can be executed by the computer..
Classification of Programs
• Binary Code: These are instruction and data that are in 0s and 1s. They are stored in the computer's main memory as they do not need any conversions.
• Octal / Hexadecimal code: These are codes or instructions that are in octal or hexadecimal representation.
• Symbolic Code: This type of programming uses symbols (numbers, letters or special characters) in place of binary code.
• High level programming language: These are codes that are directed at solving problems and not the details of computer hardware behaviours. e.g. Java, C#, Pascal etc. They need special software called interpreter or compiler to convert them into binary code
Machine Language
• These are programs that are written in binary and stored directly in memory. The octal (3 bits) or hexadecimal (4 bits) that can be easily converted to binary can also be referred to as machine language.
Assembly Language
• This uses symbols (English language words) to write instructions. This is also a low level language because of the one-to-one relationship between symbolic instruction and its binary equivalent. The symbols are referred to as mnemonics. An assembler is a special software that converts assembly language program into binary language program.
Basic symbols (mnemonics) of assembly language
Symbol Example Explanation
AND AND A Perform AND logic operation on A with value of AC
ADD ADD A Add the value of variable A to AC, and carry bit to E flip flop
LDA LDA M Load value of M into AC
STA STA M Store value of AC in M
BUN BUN TT Branch unconditionally to location TT
BSA BSA TT Save the return address in location TT and branch to TT+1
ISZ ISZ T Increment T and skip next instruction (if T = 0)
CLA CLA Clear Accumulator (AC)
CLE CLE Clear E
CMA CMA Complement AC (first complement)
INC INC Increment AC
SPA SPA Skip next instruction if AC is positive
SNA SNA Skip next instruction if AC is negative
HLT HLT Halt computer
Mnemonics of Assembly LanguageMnemonics Example Explanation
INP INP Get information from some input device
OUT OUT Put information to some output device
SKI SKI Skip next instruction if input flag is ON
SKO SKO Skip next instruction if output flag is ON
ION ION Turn interrupt ON
IOF IOF Turn interrupt OFF
CALL CALL SUB Call subroutine which starts at location SUB
RET RET Return to main program
MOV MOV A, B Transfer the value of register B to register A
Pseudo instruction (Assembler Directive)
• These are false (pseudo) instructions that do not have equivalent binary form but serve various functions. They do not refer to an operation that will be performed by the program during execution, rather it is a message to the assembler to help the assembler in the assembly process. E.g.
• ORG T, T is the memory location where the first instruction of the program must be stored.
• END, Specifies the end of the program• DEC T, T is a decimal number that needs to be converted into
binary by the assembler• HEX T, T is a hexadecimal number that needs to be converted
into its equivalent binary using 4bits.
Rules of the Assembly Language
• Each line of code must be divided into four fields. i.e. Label, instruction, operand & comment.
• Label: This is a one to three alphanumeric characters (symbolic address) that specifies the location of the instruction in memory. It should be terminated with a comma (,) to enable the assembler recognize it as a label. The first character should be an alphabet and the rest either alphabets or numerals. A line with ORG or END should not have a label.
Rules of the Assembly Language
• A symbolic address in the instruction field should specify the memory location of an operand. This symbolic address must appear again as a label, later in the program.
• Instruction field. This specifies a mnemonic (pseudo) instruction.
Rules of the Assembly Language
• Comment: Explains what each line of code does for easy understanding and explanation. Each comment must be preceded by /. This helps the assembler recognize the beginning of a program. It can be left empty.
Programming in Assembly Language
• Write a program in assembly language to store 15 at memory location T
Label Code Comment
ORG 0 /Origin of the program in location zero
LDA A /Load operand (15) from A
STA T /Store operand (15) in memory location T
HLT /Halt computer
A, DEC 15 /Decimal operand with value 15
T DEC 0 /Value 15 will be stored at T
END /End of symbolic program
Programming in Assembly Language
• Write a program in assembly language to add two decimal numbers (14 and -7) in memory locations A and B.
NB. We first have to bring the first operand to AC. Using the ADD instruction, we add the second operand with the content of AC. The result is then stored in AC. However to store the result we use STA.
Programming in Assembly Language
• . Label Code DescriptionORG 0 /Origin of program in location zero
BUN T /Branch unconditionally to location T
LDA A /Load operand in accumulator from memory location A
ADD B /Add operand from memory location B
STA SUM /Store the sum in location SUM
HLT /Halt Computer
A DEC 14 /Decimal operand with value 14
B DEC -7 /Decimal operand with value -7
SUM DEC 0 /Sum of A and B will be stored in AC, initial value of SUM is 0
END /End of symbolic program.
Flow chart
• Flow chart for adding two decimal numbers
Specify the location where the program must be stored in memory
Specify the memory location of first operand
Specify the memory location of second operand
Add second operand with first operand , already in AC
Specify the memory location where the result from AC is to be stored
Load the first operand in AC
Assembly language to subtract two numbers A – B (14 - 10)
Label Code Description
ORG 100 /Origin of program is 100
LDA SUB /Load subtrahend to AC (B)
CMA /Complement AC (-B)
INC /Increment AC
ADD MIN /Add minuend to AC (A)
STA DIF /Store the difference in memory location DIF
HLT /Halt computer
MIN, DEC 14 /Minuend with decimal 14 (A)
SUB DEC 10 /Subtrahend with decimal 10 (B)
DIF DEC 0 /Result of the difference is stored here
END /End of symbolic program.
Flow chart for subtracting two numbers A – B = A + (-B)
Specify the memory location where the program must be stored
Specify memory location of subtrahend (B)
Load the subtrahend in AC
Complement value stored in AC
Increment value in AC by 1 (get - B)
Specify location where minuend is stored in memory (A)
Add the minuend to value in AC (A +(-B))
Specify the location where the result will be stored in memory
Calculating2s complement
The negative of anumber is obtainedby calculating its2s complement. This isdone by adding 1 to its 1s complement
Assembly language- Looping
• Program loop (looping) is a sequence of instructions that are executed repeatedly, each time with a different set of data and the new result stored. E.g.
2.....Int x, y;X = 10; y = 20;If (x<y) system.out.println(“x is less than y”);x = x +2;If (x == y) System.out.println(“now x is equal to y”); }}
1)........Int x;X = 3;If (x < 10)x = x +1;else System.out.println(x); }}
3...int x;for(x=0; x<10; x++)System.out.println (“x is:”+ x); }}
Program to add 50 numbers
• To add 50 numbers we will have to use looping by repeatedly performing addition (50 times) with each of the 50 operands and storing the result in AC each time. We assume that the operands are stored in consecutive memory location so that we can get the operand by incrementing the address
Assembly language program to add 50 numbers
Line no Label Code Description
1 ORG 50 /Origin of program is HEX 50
2 LDA ADS /Load address of first operand
3 STA PTR /Store address in memory location called (PTR) pointer
4 LDA NBR /Load -50 into AC
5 STA CTR /Store the value in AC at CTR (counter)
6 CLA /Clear accumulator
7 LOP, ADD PTR I /Add an operand to AC
8 ISZ PTR /Increment pointer
9 ISZ CTR /Increment counter
10 BUN LOP /Repeat loop again, AC gets nos added and store result
11 STR SUM /Store sum
12 HLT /Halt computer
Assembly language program to add 50 numbersLine no. Label Code Description
13 ADS, HEX 50 /Address of first operand
14 PTR, HEX 0 /Location reserved for pointer
15 NBR, DEC -50 /Initialise counter
16 CTR, HEX 0 /Location reserved for counter
17 SUM, HEX 0 /Sum is stored here
18 ORG 100 /Origin of operands is from memory location 100
19 DEC 30 /First operand (i.e. 30)
.
.
68 DEC 14 /Last operand (50th number is 14)
69 END /End of symbolic program
Flow chart for adding 100 numbers
• . Specify the memory location where program is to be stored
Specify the memory location where the address of 1st operand is stored
Load this address into ACStore address (in AC) in a memory location. Let the location be PTR
Similarly, store -50 (counter variable) in a memory loc. called CTR
Set AC 0
Take address from PTR. Fetch the operand stored at this address and add it to the value in AC
Increment PTR by 1
Increment CTR by 1
Is CTR = 0 ?
Store the value in AC at location named SUM
No
Yes
Compute X+Y and store the result at Z (OR) Logic operations.
Label Code Description
ORG 0 /Origin of program at memory location 0
LDA X /Load first operand a
CMA /Complement A to get A
STA TMP /Store A in a temporary location TMP
LDA Y /Load second operand B
CMA /Complement B to get B�
AND TMP /AND with A to get A.B�
CMA /Complement (A. ) to get (A + B)B�
HLT /Halt computer
X, 1011 /First operand is stored here
Y, 1000 /Second operand stored here
TMP, 0000 /Temporary location to store A
END
Flow chart for logic OR operationLoad X into AC
Complement value in AC
Store the value of AC in TMP
Load Y into AC
Complement value in AC
AND value with TMP
Complement value in AC
Store value of AC in Z
Imperative, Declarative and Directive Statements
• Imperative statement. Indicates actions to be performed during execution of the assembled program. E.g.
LDA A, STA, ADD, ISZ C• Declarative statement. Declares constants or storage locations
or area in a program. E.g T DS 30 declares a storage area of 30 words and indicates that the
storage area must be called T. T is the first word of the operand. To access the 6th word for example, use T + 5. Alternative an index can be used. T(k) where k is the index number of item to be accessed. E.g T(6) the seventh number as it starts from 0 i.e. T(0) is the first element.
Directive
• Assembler directive statement, directs the assembler to take certain actions during the process of assembling a program. E.g. ORG ’10’ indicates that the first word of the program must be placed at memory location ‘10’. Also END indicates that no more assembly language statements remain to be processed.
Program to show all codes
.Label Code comments
ORG 0 (Directive) /Store program at location 0
LDA A (Imperative) /Load the value stored at memory location A
STA B (Imperative) /Store the value at A in the location B
A DC ‘1500’ (Declarative) /Declare a constant 1500 named A
B DS 1 (Declarative) /Declare a storage location called ‘B’ capable of storing 1 word
END (Directive) /End program