Você está na página 1de 33

Assembly Language Programming

Wichit Sirichote
Applied Physics Department Faculty of Science King Mongkuts Institute of Technology Ladkrabang e-mail: kswichit@kmitl.ac.th URL: www.kmitl.ac.th/~kswichit

Whats Assembly Language?


; my first program $MOD51 LED equ P1.7 CSEG AT 8000H jmp 8100h org 8100h PULSE: CPL LED NOP jmp PULSE END

-Assembler Controls -Assembler Directives -Instructions -Mnemonics -Operators

ASM51 Assembler

test.asm

asm51

test.lst test.hex

Test.asm
CSEG AT 8000H jmp 8100h org 8100h PULSE: CPL P1.7 NOP jmp PULSE END
4

Test.lst
8000 8000 028100 8100 8100 B297 8102 00 8103 80FB 5 6 7 8 9 10 11 12 13 14 CSEG AT 8000H jmp 8100h org 8100h PULSE: CPL P1.7 NOP jmp PULSE END
5

Test.hex
:03800000028100FA :05810000B2970080FBB6 :00000001FF :03 8000 00 02 81 00 FA :05 8100 00 B2 97 00 80 FB B6 :00 0000 01 FF : begin of record 03 number of byte 8000 load address 00 record type, 01 end of record
6

FA byte check sum= twos complement of summing from 03 - 00

8051SBC Microprocessor Learning Board


RS232C RS485 32kB SRAM 32kB EPROM LCD connector 8051 CPU Reset 12-bit ADC 8-bit input port 8-bit output port 32kB EEPROM RTC chip Debug LED 7

8051SBC Microprocessor Learning Board

16x2 LCD

www.kmitl.ac.th/~kswichit/8051sbc/8051sbc.html

8051SBC: Program Memory


FFFFH 32kB RAM 8000H 7FFFH
Interrupt vectors

64kB CSEG AT 8000H 32kB ROM

0000H

Interrupt vectors

1kB = 1024 Bytes


9

8051SBC: Data Memory


FFH
Special Function Registers

DSEG AT 30H 256 Bytes

80H 7FH 30H 2FH 20H 1FH 00H

RAM Bit Addressable RAM Register Banks

128 Bytes

10

8051s programming registers


A B SP DPTR R0-R7 PSW Accumulator register B Stack Pointer register Data Pointer register (16bit) general registers Program Status Word (Flag register)
11

8051s programming registers


P1 Port P1 P2 Port P2 P3 Port P3 TH0 TL0 16-bit timer register TH1 TL1 16-bit timer register SCON Serial Control register SBUF Serial Buffer register
12

Addressing Modes:
MOV A,#30H ;immediate mode MOV A,30H ; direct mode MOV A,R0 ; register mode MOV R0,#30H ; immediate mode MOV A,@R0 ; indirect mode MOV DPTR,#9000H ;immediate mode MOVX A,@DPTR ; indirect mode
13

Addressing Modes:
MOV C, P3.2 ; bit addressing SETB P1.7 ; bit mode SJMP $ ; relative addressing MOVC A,@A+DPTR ; index mode

14

Operation of Microprocessors:
CPU
Instruction Decoder CLOCK Program Counter RESET

Data Bus

Code Memory
02 81 00 82 97 00 80 FB

Address Bus Chip Enable

15

Instruction Fetch & Execute Cycle:


Clock A0-A15 8000H

Chip Enable

D0-D7 Fetch

02H Execute Machine cycle Synchronous bus cycle

16

Using 8051SBC for program testing


RS232C Cable

PC - Assembler -Terminal emulator

9600 8n1

8051SBC

9600 bit/sec data 8bits no parity one stop bit

17

Serial Port: RS232C


TTL logic UART TxD RxD RS232 Logic TTL logic UART RxD TxD

Level converter

UART (Universal Asynchronous Receiver and Transmitter)

18

Serial loading through RS232C


RS232 logic

COM1

8051SBC

19

NRZ Digital Baseband


START BIT +5V 0V D0 D1 TTL NRZ signal D2 D3 D4 D5 D6 STOP BIT D7

1 bit length = 1/bit per second 1 Frame = 10 bits +10V 0V -10V RS232C signal

20

Sending letter A to terminal


LOOP: MOV A,#A ; A = 41H CALL putch JMP LOOP JNB TI, $ CLR TI MOV SBUF,A RET ; TI transmitter ; empty flag ;serial buffer reg

putch:

21

Sending letter A to terminal


AAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAA AAAAAAAAA ADDR:8000>_

Press RESET

22

Sending string Hello to terminal


Hello
Loop: A = [MEM] if (A== eos) exit send A MEM++ jump loop

H [MEM] e l l o
eos
23

Subroutine putstring
putstring: CLR A MOVC A,@A+DPTR CJNE A,#eos,string1 RET string1: CALL putch INC DPTR ; next address JMP putstring eos equ 0 ; terminator
24

Subroutine putstring
Main: MOV DPTR,#hello CALL putstring JMP Main DB hello,13,10,eos

hello:

13 = 0dH is ASCII printing control code. It will move cursor back to the left most position! (Carriage Return) 10 = 0ah is Line Feed, LF. The cursor will move to new line!
25

Simple delay subroutine


delay: MOV R6,#100 delay1: MOV R7,#0 Inner outer DJNZ R7,$ loop loop DJNZ R6,delay1 RET ; inner loop will be repeated 100 times! ; simple coding, but waste CPU time!

26

Measuring delay period


delay: MOV R6,#100 DJNZ R6,$ RET ; we can determine delay period with simple code and a given output port! main: CPL P1.7 CALL DELAY JMP main
27

Measuring delay period


P1.7 DSO P1.7

Frequency counter T = 1/f delay period = T/2


28

Using Hardware Timer


16-bit Timer0: TH0 TL0
TF0 TH0 0000 0000 0000 0000 0000 0000 . . 1111 1111 0000 0000 OSC 1/12 TL0 0000 0000 0 0000 0001 1 0000 0010 2 . . 1111 1111 65535 0000 0000 0
29

TF0=1

Using Hardware Timer


8051SBC has 11.0592MHz oscillator. The clock frequency for timer is 11.0592MHz/12 = 921,600 Hz If we have timer0 with initial value = 0000h, TF0 will set when time equal [1/921,600Hz] x 65,536 = 0.07111 s has elapsed.
30

How to set MODE for Timer0?


TMOD (89H) : Mode setting for timer TCON (88H): run/stop timer counting
GATE C/T M1 M0 Timer1 Timer0

MODE1 = 16-bit timer (M1=0,M0=1) ORL TMOD,#1 ; logical OR TMOD with 1


31

How to run Timer0?


TCON (88H): run/stop timer counting

TF1

TR1 TF0 TR0 IE1

IT1

IE0 IT0

; run timer0 by setting TR0 to 1 ORL TCON,#00010000B ; or with set bit instruction SETB TCON.4
32

Sample code with timer0


Main: ORL TMOD,#1 SETB TCON.4 JNB TF0,$ CLR TF0 CPL P1.7 JMP wait

wait:

33

Você também pode gostar