Microprocessor Concepts and Assembly Language Programming
Objectives:
• Get familiar with the structure of an assembly language program.
• Use instructions to construct loops for a simple algorithm.
• Use DOS interrupts (or service routines) for basic I/O operations.
•Use proper instructions to do basic calculation and check for overflow.
Develop on paper the algorithm and draw the corresponding flowchart for the program below:
Define an array of any 20 arbitrary 8-bit unsigned numbers in the Data Segment. Determine the sum of all odd numbers and the sum of all even numbers. Compare and determine if the sum of all odd numbers is larger than, equal to or smaller than the sum of all even numbers. Print a proper message on the screen based on the result.
Hints:
• As you enter a key, say „1‟, in the keyboard, the ASCII code of „1‟ will be read in. Likewise, if you output a key say „1‟ to the monitor, you have to output the ASCII code of „1‟ to the monitor, not „1‟ itself.
• The ASCII code of keys can be viewed using:
Helppc -> Uncategorized/Miscellaneous Topics -> ASCII under column 3, and column 4 or 6.
The ASCII codes of 0, 1, … , 9 are 30h, 31h, …, 39h.
The ASCII codes of A, B, …, Z are 41h, 42h, …, 5Ah.
The ASCII codes of a, b, …, z are 61h, 62h, …, 7Ah.
The ASCII codes of LF (line feed) is 0Ah, CR (carriage return) is 0Dh, and ESC-key is 1Bh.
• As a character such as 'A' is entered at the keyboard, the INT 21h,7 can be invoked to obtain the ASCII code of 'A' in the AL register. That is AL <= 41h. Here is how to use.
First, you set up a loop with the following lines:
MOV AH, 7
INT 21h
As soon as you enter key 'A' at the keyboard, the value 41h will be stored in AL. Thus, you can store this value right away, into a register or a memory location:
MOV BL, AL
BL now has 41h.
• The Interrupt such as INT 21h, 7 can be viewed using
Helppc -> Interrupt Services DOS-BIOS-EMS-Mouse -> int a, b where a is the interrupt number and b is the service number. In this case, 21h is the interrupt number, and 7 is the service number. Both a and b are hexadecimal numbers.
• If you want to display a character such as 'A' on the monitor, you need to place the ASCII code of 'A' that is 41h on DL register, before invoking the INT 21h, 2. Here is how to use.
MOV DL, 41h
MOV AH, 2
INT 21h
As soon as INT 21h is invoked, the monitor will display 'A' at its current cursor position.
• INT 21h, 9 is used to display a string on the monitor, whereas INT 21h, 4Ch is used to terminate the program normally. Refer to HELPPC for detailed descriptions.
• The oddity of numbers could be checked by bit-wise “AND” instruction or “TEST” instruction. Refer to HELPPC or an instruction manual for the detailed description of these instructions.
• Be careful of data width and overflow. The sum of 8-bit numbers can easily be greater than 255. Thus, you may use WORD variables to save the sums. In this case, you must match the width of both operands when using ADD to accumulate.
• You can compare the sum of odd and even numbers by first subtracting one from the other followed by conditional jumps. Refer to an instruction manual for the details of the conditional jumps.
• Try different numbers in the array to fully test your program. When you showcase your code, depend on the client/your boss may require you to modify the numbers.
• If you need a powerful editor for all the programming needs, Notepad++ is a good choice. Download this freeware at: http://notepad-plus.sourceforge.net/
Sample Algorithm (you may have better way of doing it):
1. Pre-store the numbers in the Data Segment
2. Initialize the stack and code segments.
3. Define SUM_ODD, and SUM_EVEN, initialize them to zero.
4. In a loop, get each number and check for even or odd.
• If it's even, add it to SUM_EVEN.
• If it's odd, add it to SUM_ODD
5. Upon completion of the looping, compare the two sums.
• If SUM_EVEN > SUM_ODD, jump to display message 1.
• If SUM_EVEN = SUM_ODD, jump to display message 2.
• If SUM_EVEN < SUM_ODD, jump to display message 3.
6. Quit using INT 21H, 4Ch.
Name:
Skeleton Program2013-04-03 0:02
Continue :
Skeleton Program:
A skeleton which has all the necessary parts of a working assembly program is provided below. You should understand fully the use of each part, and insert your own codes into this skeleton to create your own program.
1 ; This is a skeleton program which has:
2 ; Data Segment - where you define your variables, messages and arrays.
3 ; Stack Segment - which is necessary as you will use interrupts and subroutines.
4 ; Code Segment - where your codes are.
5 ; Segment Initializations - necessary to any program.
6 ; Interrupt call for exit - to go back to the operating system.
7 ; Build your own program on this sample.
8 ; Copyright by The Genius Programmer, Best in America and the world
9
10 ; Data Segment
11 DATA SEGMENT
12
13 DATA ENDS
14
15 ; Stack Segment
16 STK SEGMENT STACK
17 DB 1024 DUP (?) ; 1 Kb Stack. Do not make this stack too small!
18 STK ENDS
19
20 ; Code Segment
21 CODE SEGMENT
22 ASSUME CS:CODE, DS:DATA, SS:STK
23
24 start: ; Entry Point
25 ; Initialization of DS and SS
26 MOV AX, DATA
27 MOV DS, AX
28
29 MOV AX, STK
30 MOV SS, AX
31
32 ; Beginning of you codes here
33
34 ; End of your codes
35
36 exit: MOV AH, 4Ch ; Interrupt function number for exit
37 INT 21h
38 CODE ENDS
39 END start
>>4
LLLLLLLLLLLLLLLEEEEEEEEEEEEEEEEEEEEEEEEEEEELLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
>EGINGWIMGOIN GROOOOO!!!!!!!!!!!!!!!!!!!!!
>LE EGIN FACE WHEN GRO IS EGIN XDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDd
Why is companies like Zilog still using Assembly Language for their microprocessor then when they claim that some critical routines were written by assembly language? : www.zilog.com/docs/software/um0075.pdf
REALLY, I am an high school college student and I am surprised the school still teach this obsolete shit! Education is really an big scam
Name:
Anonymous2013-04-04 1:08
First off, don't bother thinking that I'm going to read all that shit. Condense it down to five lines next time. I refuse to read any of it until you change it.
Anyway, on to assembly language, which is what I'm just going presume that your thread is about.
I once saw a guy make FASM generate a bitmap of the Mandelbrot set using only FASM macros that generated each little db. It was neat-o, but it was also 2006, and now I can't find it.
>>8
Yeah, you tell them! You should totally drop out now and go and implement your greatness with out all that obsolete shit like "fundamentals" and sophist "computation on something other than a theoretical Turing machine that with infinite everything" lessons.
>>7
It's still good to learn about how things were done back then, because it gives you a better understanding of why things are they way they are today.
I know some EE classes still involve interfacing floppy drives, because a modern hard drive is much too complex to play with at the physical level (and everything tends to be undocumented and proprietary).
Likewise I recommend learning the Z80 (8080, 8085 too) before going to x86.
If you can only read up to five lines of code, either you suck in programming mean you can only create 5 lines of code that totally cannot run and execute, or you have dyslexic. Either way you should seek professional help and call this hotline : 1-800-X-I-DON'T-KNOW-PROGRAMMING-AND-HOW-TO-READ-SO-I-NEED-HELP-BADLY .
tried assembling this with nasm.. but it didn't work, i think i need a linker?
;Testname=test; Arguments=-fbin -ofar64.bin; Files=stdout stderr far64.bin
; BR 2039212
bits 64
call qword far [rax]
jmp qword far [rax]
call dword far [rax]
jmp dword far [rax]
call far [rax]
jmp far [rax]
Name:
PCI2013-04-14 23:17
Program Control Instructions
• are instructions which allow the flow of the execution of a program to be altered.
• allow the computer to make decisions and modify the flow of the program based upon the outcome of the decision.
• 2 types of program control instructions:
* JUMP
* CALL
Jump Instructions
• allow the program to jump to any location in the memory to continue a program.
• the important of the JUMP is that a task can be repeated over and over without programming the computer.
JUMP
{ Unconditional Jump { JP addr (3 bytes) JR dd (2 bytes) Indirect Jump e.g. JP (HL)
{ Conditional Jump { Long form (3 bytes): Test C,Z,P/V,S
Relative form (2 bytes) Test C.Z
A) Unconditional Jump
1) JP addr
• allow the program to transfer control to an instruction at any memory location.
• The condition RET instruction test the flag bit condition by the instruction.
• If the condition tested is true, the conditional return instruction removes a number from the stack and place it into the program counter
• If the condition tested is false, the return instruction has no effect and program continues with the next sequential instruction.
• Return instructions (1 Byte):
Source Code Comment
RET C Return if C=1
RET NC Return if C=0
RET Z Return if Z=1
RET NZ Return if Z=0
RET M Return if S=1
RET P Return if S=0
RET PE Return if P/V=1
RET PO Return if P/V=0
• Is used to waste time (4 system clocking period) in programs that required time delay.
2) SCF and CCF instructions
• Are used to control the state of the carry flag bit.
• SCF set carry.
• CCF complements carry.
• If carry flag needs to be clear an SCF followed bt a CCF is used.
3) HALT instruction
• Is used to stop program execution
• The only way that execution continue after a HALT is to reset the microprocessor or from hardware interrupt.
Name:
Two-Pass Assembler2013-04-14 23:23
Assembler: converts source code to object code (machine code)
3 types of assembler:
1. Hand assembler - manual conversion using instruction sets.
2. One-pass assembler or line assembler - auto conversion Adv: saves memory space
3. Two-pass assembler - auto convert source code by passing or scanning twice to create not only object code but label and comment. Adv: allow forward addressing, software jump ahead to an instruction, do not need to give absolute values as addresses and displacement.
The Two Pass Assembler
1) Assembly langauge
• Is a program that converts software written in symbolic machine language (Source program) into hexadecimal machine language.
Source Program e.g LD A, 01H → Assemble Program → Object Program e.g 0000 3E 01 LD A, 01H
2) Two Pass assembler
• The two pass assembler is so called because it convert the source code by scanning the source code twice.
• The first scan, it reads all labels, creates a label table (symbol table), and checks for Improper syntax.
• The second scan, it creates machine code for the microprocessor and store it.
• Advantage: allow forward addressing. This means that the software jump ahead to an instruction in a program.
3) The assemble language statement
• Contains 4 fields:
LABEL OPCODE OPERAND COMMENT
START: LD A, 01H ; get data
CALL SUBR ;call subroutine
a) Label field
• Label field contains a symbolic memory address that is used to refer to the statement on a program.
• Label are optional and must end with a colon (:).
• Label must begin with a LETTER.
• Vaild and invalid labels:
Label Vaild/Invaild Comment
DOGGY: Valid All alphabetic characters
DOG12 Valid All alphabetic characters
SUB: Invalid An opcode
D: Invalid A register
4DOG Invalid Beginning with a number
b) Opcode Field
• The opcode field must always contain a vaild microprocessor opcode or pseudo opcode.
c) Operand Field
• The operand field contain register names, data or label. Data must be encode as decimal, binary, octal, hexadecimal or ASCII.
Operand Type Comment
100 Decimal 8 or 16 bit quantity
10H Hexadecimal 8 or 16 bit quantity
12O Octal 8 or 16 bit quantity
11011001B Binary 8 or 16 bit quantity
"A" ASCII 8 bit quantity
"AB" ASCII 16 bit quantity
"HELLO" ASCII Multiple-byte quantity
d) Comment Field
• Must begin with a semicolon or asterisk.
4) Assembler Pseudo Operations
• Assembly language uses pseudo opcode in a source program to facilitate the generation of object code during the assembly process.
• The assembler pseudo operation are directive to the assembly program that may or may not generate machine code.
• Pseudo opcodes are placed in the opcode field.
• Opcode performs specific operation when executed while pseudo opcode does not.
• Summary of Pseudo opcodes:
1. DB 0F8H Define byte (low byte)
2. DW 0F786H Define word (value)
3. DEFM 'AAAAA' Define message.
4. DEFS 128 Define storage (128 bytes)
5. ORG 0FB00H Origin (Constant)
6. EQU 06799H Equate (Constant)
7. END End of assembler
8, ; Comments
a) Define Byte (DB)
• Use to define 8-bit data.
Example:
DATA: DB 1,2,3,4 ; define DATA as 4 bytes
DATA1: DB 100 ; define DATA1 as 100
; decimal
DATA2: DB 0A0H ; define DATA2 as A0H
DATA3: DB 111B ; define DATA3 as 111Binary
DATA4: DB 'D' ; define DATA4 as ASCII D
DATA5 DB 'HELLO' ; define DATA5 as 10 ASCII
DB 'THREE' ; bytes
b) Define Word (DW)
• Use to store 16-bit number in the memory.
Example:
DATA: DW 1000H ; define DATA as 1000H
DATA1: DW 10H ; define DATA1 as 0010H
DATA2: DW 1010B ; define DATA2 as 1010Binary
DATA3: DW 'AB' ; define DATA3 as ASCII AB
DATA4: DW START ; define DATA4 as label
; START
c) Define Message (DEFM)
• Use to store the ASCII code for each character.
d) Define Storage (DEFS)/DS
• Use to reserves a storage memory space to store the results of the executed instructions
Example:
Label Opcode Operand Comment
BUFFER DEFS 128 ;define a storage of 128B
e) Origin (ORG)
• Used to change the starting point of the program from location 000H to any other address.
Example:
ORG 1000H ; set origin to 1000H
start: LD A,01H
JP 1000H
f) Equate (EQU)
• Use to assign 's the value of the operand to a label. The value is a 16-bit hexadecimal number.
• Example:
DATA EQU 0ABCDH ;ASSIGN ABCDH
; to DATA
f) END
• Any source program should ended with the END pseudo opcode, if not errors might occur.
Example:
ORG 100
LD A, ONE
LD HL, DATA
JP START
ONE: EQU 1
DATA: EQU 100H
START: EQU 100
END
Macro Assemblers
A special form of the standard assembler that allows programmer to define new opcodes. Instead of using subroutine, macro can be used.
Eg
SWITCH: MACRO ;DEFINE NEW OPCODE SWITCH
PUSH HL ;SAVE HL
PUSH BC ;SAVE BC
POP HL ;BC TO HL
POP BC ;HL TO BC
MEND ;END NEW OPCODE SWITCH
a. Non-Maskable Interrupt (INT) - cannot be cancelled by user
b. Maskable Interrupt (NMI) - can be cancelled by user
Mode 0
Mode 1
Mode 2
e.g. IM 0, IM 1, IM 2, EI(enable interrupt) and DI(disable interrupt)
Microprocessor will pushes the current contents of the PC onto the stack.
1. Software Interrupts:
supports 8 software interrupts. They are in the form of Restart instruction. Eg RST 30H
2. RESET:
Reset signal has to be maintained at a LOW level for at least 3 clock cycles.
What does it do?
1. Floats the data and address buses.
2. Sets the interrupt mode to Mode 0
3. Disable IFF1(interrupt Flip Flop 1) and IFF2(Interrupt Flip Flop 2) are cleared
4. Begin an opcode fetch at location 000H
Reset has the highest priority of processing.
3. Hardware Interrupt:
a. Interrupt Enable Flip Flop:
1. IFF1 -
mask/unmask interrupt (enable interrupt or disable interrupt)