Title: CSCI130
1Lecture 8
- CSCI130
- Instructor Dr. Imad Rahal
2Announcements
- Practice set 3
- Exam 1 review set
- unclaimed quizzes
3(No Transcript)
4Assembly Language
- Machine code consists of the
- binary instructions, data addresses
- can directly be executed by the CPU
- We as humans are not good in working with
sequences of 1s and 0s - We prefer to use an English-like style (English
mnemonics and decimal numbers) - This is called the assembly language
- It has 1-to-1 correspondence to machine code
- one instruction for every 3-bit Opcode and
decimal numbers for addresses and data - Slight other changes
5Assembly Language
- Additional problems addressed
- Most commands like LOAD, STOR, JUMP and ADD
require memory addresses - How do we know which address in memory is free to
use? - We also assumed that PC will contain 0 initially
- Our program will be loaded to first memory
locationis this always true? - What if we have multiple executing programs on
the same machine? - Which will get the 0 address then?
6Assembly Language
- In reality, when we program, we dont concern
ourselves with such low level details - More on this in the next chapter (OS)
- From now on, we use variables/labels instead of
addresses and totally forget about memory - 110 10010 ? ADD Counter which must initialized
7Add all numbers between 1 and 10
- Algorithm
- Sum 0, decrement1, Limit10
- Add limit to sum
- Subtract decrement from Limit
- If Limitgt0
- Go back to step 2
- Display sum
- Stop
8Assemblya nicer way to write the same thing
START LOAD SUM ADD LIMIT STOR
SUM LOAD LIMIT SUB DECREMENT STOR
LIMIT JPOS START LOAD SUM WRITE (
STOR 31) HALT SUM 0 DECREMENT 1 LIMIT
10
Changes 1- Use variables for needed data
holders 2- Use labels for jumps 3- WRITE for STOR
31 READ for LOAD 30
9Add all numbers between 1 and N
- Algorithm
- Input N
- Sum 0, decrement1, limit N
- Add limit to sum
- Subtract decrement from Limit
- If Limitgt0
- Go back to step 3
- Display sum
- Halt
10Program to add all numbers between 1 and N
READ //instead of LOAD
30 STOR LIMIT LOOP LOAD SUM
ADD LIMIT STOR SUM LOAD LIMIT SUB
DECREMENT STOR LIMIT JPOS LOOP LOAD
SUM WRITE //instead of STOR 31 HALT SUM
0 DECREMENT 1 LIMIT 0
11Assembly Language
- Computers can only understand machine code
- Programs written in assembly must be translated
to machine code to be executable by CPU - The assembler is responsible for that
- Stored in memory and executed before any assembly
program can be executed - (english-like) assembly source code ? (binary)
machine code - Does a lookup for each word (Opcodes)
- Other things (find empty memory locations for my
variables) - The result is called object code
12Program to find the larger of two input numbers
- 0 READ ( LOAD 30) loads 1st number
- 1 STOR X store 1st number at location X
- 2 READ loads 2nd number
- 3 STOR Y store 2nd number at location Y
- 4 SUB X subtract 1st from 2nd
- 5 JPOS Y_LARGER if AC isgt0, then 2nd num is
larger - 6 LOAD X otherwise, the 1st num is larger
- 7 WRITE output first number
- 8 HALT program is done
- 9 Y_LARGER LOAD Y
- 10 WRITE Output result
- 11 HALT End program
- 12 X 0 Initialize variables
- 13 Y 0
13More Practice
- Algorithm and program to print all numbers
between two user-input numbers N1 and N2
inclusively (downwardsassume N2 gt N1)