CS计算机代考程序代写 assembly Quiz No. 2

Quiz No. 2
Q: An embedded system designer wishes to build an embedded system to be installed in a manufacturing machine. The hardware of the embedded system contains an 8-bit CPU and the following memory chips (total 4):
Data memory (RAM) 3 modules (chips) size: 4KB each
Program memory (ROM) 1 module, size: 2KB
a) Design the addressing circuit and bus structure used to connect the CPU to the memory unit. Show a schematic diagram of your design with proper connections between the hardware components.
b) Describe the resulting memory address map and additional hardware you need to realize the addressing unit.
Solution:
a)Addressing circuit:
We have a total of (2+4+4+4) = 14KB addressable memory.
The number of address lines necessary to access 14KB is
L = log2 14000 > 13. Therefore, we need 14 address lines
Since we have 4 memory chips, we need:
Log2 4 = 2 lines for chip selection. The rest of the address lines are shared by all memory chips and used for internal addressing within a single chip.
Address bus (A0 .. A13) A0… A11 for internal addressing; A12 and A13 for chip selection
CPU
2 to 4 Decoder
ROM Chip 2KB
(0)
RAM Chip 4KB (1)
RAM Chip 4KB (2)
RAM Chip 4KB (3)
Address bus (in chip address, 12 bits)) Chip decoder (chip selector, 2 bits) Data bus (8 lines)
Control bus (2 lines, R/W)
Chip Select 00
01 10 11
b)
Chip#
0 (ROM) 1 (RAM) 2 (RAM) 3 (RAM)
Memory Address map: Starting
0
4096
8191
12288
Ending 2047 8191 12287 16383

It is also possible to reverse the order of the chips as follows:
Chip# Starting 0 (RAM) 0
1 (RAM) 4096
2 (RAM) 8191
Chip Select 00
01
10
Ending 4095 8191 12287 14335
3 (ROM) 12288
Additional Hardware needed to implement the addressing system is a 2-to-4 decoder.
The difference between the two arrangements is that there is unused(unusable) address space in Chip0 for the first case, while we have the same for the second case in Chip3.
** Additional Hardware needed to implement the addressing system is a 2-to-4 decoder.
11

Q2: Write assembly code to perform the following tasks on ATMega328 microcontroller.
i)
ii)
Send the decimal value 45 to output via port D
iii)
Get the numbers to be added as in (ii) above from SRAM address 0210H and 0220H, perform addition and store the sum at address 0230H.
Solution:
We shall use the 16-bit X register (R27:R26) for copying the data from/to memory In our case, X=0210 for the first data. Hence, R27 = 02 and R26 = 10 (hexa)
; get the first number
LDI R17, 0b11111111 Out DDRD, R17
LDI R17, 45
OUT PORTD, R17
;immediate addressing to copy data to R17
; initialize port D control register for output on all pins (0b1111 1111) ; immediate addressing to copy value to output
; send the data to PORTD buffer (data) register
Add two 8-bit integers on registers R0 and R1 (e.g. 45 + 35)
LDI R17, 45 MOV R0, R17 LDI R17, 35 MOV R1, R17 ADD R0, R1
; immediate addressing to copy value to R0 ; initialize R0 to 45
; immediate addressing to copy value to R1 ; initialize R1 to 35
; (R0 = 45+35)
LDI R27, $02
LDI R26, $10
LD R0, X
;get the second number
LDI R27, $02 LDI R26, $20 LD R1, X ;add
ADD R0, R1 ;store the result LDI R27, $02 LDI R26, $30
ST X, R0
; immediate addressing to copy high side of memory address to R27 ; immediate addressing to copy low side of memory address to R26 ; get the first data from memory pointed by X to R1
; sum = R0 = R0 + R1
; immediate addressing to copy high side of memory address to R27 ; immediate addressing to copy low side of memory address to R26 ; Store the result R0 to memory pointed by X
; immediate addressing to copy high side of memory address to R27 ; immediate addressing to copy low side of memory address to R26 ; get the first data from memory pointed by X to R0
Note: You can use the Y or Z register as well as a pointer.

adder:
CLR R0
CLR R1
LDI R16, 15
ADD R0, R1 INC R1
CP R1, R16 BRNE adder
;Set R0=0 (this is quicker than using R17) ;SetR1=0
; initialize the number of iterations – 1
; R0 = R0+R1 (first time it is 0) ; R1++
; is R1=15?
; repeat the loop if not yet=15
iv)
Send the number 30H to PORTD if R0 contains the value 50. Otherwise send the number 70H to PORTD.
Solution:
This is a branching/decision problem (e.g. if (R0 == 50)
isnot50:
endprog:
CPI R16, 50
BRNE isnot50
LDI R17, 0b11111111 Out DDRD, R17
LDI R17, $30
OUT PORTD, R17 JMP endprog
LDI R17, 0b11111111 Out DDRD, R17
LDI R17, $70
OUT PORTD, R17
; check if R0 = 50?
; if not, skip/jump to the label isnot50
;immediate addressing to copy data to R17
; initialize port D control register for output on all pins (0b1111 1111) ; immediate addressing to copy value to output
; send the data to PORTD buffer (data) register
; skip the next lines because they are executed when R0=50
;immediate addressing to copy data to R17
; initialize port D control register for output on all pins (0b1111 1111) ; immediate addressing to copy value to output
; send the data to PORTD buffer (data) register
v)
Find the sum of the first positive integers under 15.
Solution:
This corresponds to the high level code: int sum = 0; for (int i=0; i<15; i++) sum=sum+i. • Initialize one register to 0 (to hold the sum) • Initialize another register to 0 (to be incremented and added to the first register) • Add the two registers • Increment the second register • Check if the second register is equal to 15 • If not, repeat addition, otherwise end the program endprog: Note: You can use the code for PORTD if you want to send the sum to output vi) Modify the example in (v) above such that a sub routine called dataout is used to send the sum to output (PORTD). CLR R0 CLR R1 LDI R18, 15 ADD R0, R1 INC R1 CP R1, R18 BRNE adder MOV R16, R0 CALL dataout JMP endprog LDI R17, 0b11111111 Out DDRD, R17 OUT PORTD, R16 RET endprog: Additional Exercises: ;Set R0=0 (this is quicker than using R17) ; Set R1 = 0 ; initialize the number of iterations -1 ; R0 = R0+R1 (first time it is 0) ; R1++ ; is R1=15? ; repeat the loop if not yet ; copy the result to the register used for calling the dataout routine ; call the data output sub routine ; skip the next lines and go to the end of program ;the data to be output is available on R16 ;immediate addressing to copy data to R17 ; initialize port D control register for output on all pins (0b1111 1111) ; send the data to PORTD buffer (data) register adder: dataout: i) Write a sub routine called intadder that returns the sum of the first N numbers. N is read from SRAM at address 0120H. ii) Write a program that calls intadder to add the integers and displays the result using the subroutine dataout above. iii) Write a program that copies the first 10 bytes of data