ECTE333汇编代码合集

asember_code


Lab1-b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
;
; Lab1-b assembler program
;
.include "m8515def.inc" ; include the ATMEGA8515(L) definitions file
.org $000
rjmp RESET
.org $007 ; set start address to Timer 0 overflow
rjmp ISRTIME ; jump if timer0 overflows
.def outval = r16 ; labelling regisers
.def temp = r17 ;
.def counter = r18 ;
RESET:
; Initialise the stack pointer (standard method)
ldi temp,low(RAMEND) ; lower half
out SPL,temp
ldi temp,high(RAMEND) ; upper half
out SPH,temp
; set up portb
ser temp
out DDRB, temp ; set all pins of PORTB as outputs
ldi outval, 0xAA ; set to zero
out PORTB, outval ; output to port B
; initialize timer 0
ldi temp, 0x05
out TCCR0, temp ; set timer 0 prescaling to lowest /1024
clr temp ; clear all bits
out TCNT0, temp ; clear the timer
; setup timer 0's overflow interrupt
in temp, TIMSK ; load Timer Interrupt Mask Register
sbr temp, 1<<TOIE0 ; set the timer 0 overflow interrupt enable bit
out TIMSK, temp ; save Timer Interrupt Mask Register
ldi counter, 0x04
sei ; enable interrupts
LOOP:
; do nothing, just loop forever(waiting for timer0 overflow)
rjmp LOOP
ISRTIME:
; interrupt service routine
; is executed every 2^16 pre-scaled cycles
dec counter
cpi counter, 0x00
brne BACK
com outval ; Toggle ODD and EVEN Pins
out PORTB, outval ; output to port B
ldi counter, 0x04
BACK:
reti ; return from interrupt (different to normal return)

andi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
;
; assembler program
;
.include "m8515def.inc" ; include the ATMEGA8515(L) definitions file
; Some register definitions, using meaningful names
.def BCD1 = r16 ; define register to store the first BCD value
.def BCD2 = r17 ; define register to store the second BCD value
.def temp = r18 ; use this register to store temporary values
; Define a label "reset". Jump here if you want to reset the program
RESET:
; Setup PORTA as an input and PORTB and PORTC as outputs.
ldi temp, 0b11111111
out DDRB, temp ; set all pins of PORTB as outputs
out DDRC, temp ; set all pins of PORTC as outputs
ldi temp, 0b00000000
out DDRA, temp ; set all pins of PORTA as inputs
LOOP:
; Now we continuously run the program in a loop
in temp, PINA ; Read the pin values of PORTA into register
; Extract BCD1 value (bits 0-3)
com temp ; Invert the bits for use on hardware
mov BCD1, temp ; Copy the value of temp into BCD1
andi BCD1, 0b00001111 ; Mask off the least-significant 4 bits
; Extract BCD2 value (bits 4-7)
mov BCD2, temp ; Copy the value of temp into BCD2
andi BCD2, 0b11110000 ; Mask off the most-significant 4 bits
swap BCD2 ; Swap the nibbles so that the 4 bits
; are in the correct position
; Add values and output to PORTB
mov temp, BCD1 ; temp = BCD1
add temp, BCD2 ; temp = temp + BCD2
com temp ; Invert the bits for use on hardware
out PORTB, temp ; Write the result of addition to PORTB
; Multiply values and output to PORTC
mov temp, BCD1 ; temp = BCD1
mul temp, BCD2 ; r1,r0 = temp * BCD2
mov temp, r0 ; r0 lower byte (small numbers)
com temp ; Invert the bits for use on hardware
out PORTC, temp ; Write the result of the multiply to PORTC
rjmp LOOP ; Jump back to start
; END of code

不改变register 写入mem[]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
;
; 基本操作
;
.include "m8515def.inc" ; include the 8515 definition file
.def temp = r16 ; define temporary registers
.def temp2= r17
.def temp3= r18
.def temp4= r19
.def temp5= r20
.def temp6= r21
.def temp7= r22
ldi temp, 0b00000100 ;赋初始值
ldi temp2, 0b00001001
ldi temp3, 0b00000000
ldi temp4, 0b00000000
ldi temp5, 0b00000001
ldi temp6, 0b11111111
ldi temp7, 0b00000000

sts 0x0060, temp ;Mem[0x0060]
sts 0x0061, temp2 ;Mem[0x0061]

; additional operations
add temp3, temp ;连加处理add指令
add temp3, temp2 ;add no carry
sts 0x0062, temp3 ;adc carry

add temp4, temp ;unsigned and overflow
sub temp4, temp2
sts 0x0063, temp4


mul temp5, temp ;
mul temp5, temp2
sts 0x0064, temp5

and temp6, temp ;logic and
and temp6, temp2
sts 0x0065, temp6

or temp7, temp ;logic or
or temp7,temp2
sts 0x0066, temp7

Q:What values do you expect to be stored in each of registers r18 through r22 for the first program?
• Did you obtain your expected values?
• Why were the registers used, starting from r16?
• Which instructions limit the use of the destination register?
• Was there any Carry, Negative or Overflow issues observed in the operations?
• Where were these reflected?
• In Task 3, what instructions were used to skip over the unneeded branches of the code?


read in from pins store to memory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
;
; Lecture 3 example assembler program
;
.include "m8515def.inc" ; include the 8515 definition file
.def temp = r16 ; define temporary registers
.def temp2 = r17
;
; this is an example code for lecture 3
;
RESET:
; Set the Data Direction Register (DDRA)
; (0's = inputs, 1's = outputs)
ldi temp, 0x00
out DDRA, temp
; read in from PortA pins store to memory location 0x0200
in temp, PINA
sts 0x0200, temp
; read in from PortA pins store to memory location 0x0201
in temp2, PINA
sts 0x0201, temp
; add values together and store to location 0x0202
add temp, temp2
sts 0x0202, temp
; load an immediate value and store to location 0x0203
ldi temp, 0x54
sts 0x0203, temp
; load another immediate value and store to location 0x0204
ldi temp, 0xF0
sts 0x0204, temp
; subtract an immediate value and store to location 0x0205
subi temp, 0x31
sts 0x0205, temp
LOOP:
; Continually loop doing nothing
rjmp LOOP ; jump back to LOOP

if
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

.include "m8515def.inc" ; include the 8515 definition file
.def temp = r16 ; define temporary registers
.def temp2 = r17
;
ldi temp, 0x00 ;PINA input , PINB output
out DDRA, temp
ldi temp, 0xFF
out DDRB, temp
;
int main(void) { ;不知道正确性
for(::)
{
in temp, PINA
if( r16==1 )
ldi temp2, 0b00000101
else if(r16==2)
ldi temp2, 0b00000110
else if(r16==3)
ldi temp2, 0b00000111
else
ldi temp2, 0b00001000
out temp2,PINB
}
}