Extracted Notes For Mid Sem
Extracted Notes For Mid Sem
Sir, let's break down these concepts step by step, focusing on the memory architecture and key concepts related
to the AVR Atmega32 microcontroller. I'll guide you through visualizing the memory map, registers, I/O ports,
stack, etc.
1. Memory Map of Atmega32
AVR microcontrollers like the Atmega32 have 3 main types of memory:
• Flash Memory (Program Memory): Stores the program you write in assembly or C.
• SRAM (Data Memory): Stores variables, stack, and I/O register data.
• EEPROM: Stores non-volatile data that needs to be saved after power off.
Here’s the layout:
css
Copy code
| Memory Type | Address Range | Size |
|-----------------|-----------------------|------------|
| Flash (Program) | 0x0000 to 0x3FFF | 32 KB |
| SRAM (Data) | 0x0060 to 0x085F | 2 KB |
| EEPROM | 0x0000 to 0x03FF | 1 KB |
Flash Memory
• Used to store your program (instructions).
• Atmega32 has 32 KB of flash memory (addresses: 0x0000 - 0x7FFF).
SRAM (Data Memory)
• Contains general-purpose registers, I/O registers, data memory (variables), and stack.
• Size: 2 KB (from 0x0060 to 0x085F).
Let's break down the SRAM:
2. Registers and I/O Ports
General-Purpose Registers (R0 - R31):
• R0 to R31 are 32 general-purpose registers.
• Size of each register: 8 bits (1 byte).
• Memory address of registers: They occupy addresses from 0x0000 to 0x001F in the SRAM.
Visualization:
css
Copy code
R0 -> Address 0x0000 (8 bits)
R1 -> Address 0x0001 (8 bits)
...
R31 -> Address 0x001F (8 bits)
I/O Registers:
• The I/O registers are mapped in memory from 0x0020 to 0x005F.
• They control I/O operations like ports (PORTA, PORTB, etc.) and timers.
Example I/O Ports:
○ PORTA: 0x003B
○ PORTB: 0x0038
○ PORTC: 0x0035
○ PORTD: 0x0032
Each I/O register is 1 byte (8 bits), meaning you can write an 8-bit value to control the pins (0 or 1).
Example:
css
Copy code
0x085F (Top of SRAM)
↓
0x0060 (Bottom of SRAM)
vbnet
Copy code
PORTA (0x003B): 8 pins PA7 to PA0
PORTB (0x0038): 8 pins PB7 to PB0
PORTC (0x0035): 8 pins PC7 to PC0
PORTD (0x0032): 8 pins PD7 to PD0
5. Stack Operations
• PUSH: Store data on the stack. The Stack Pointer (SP) decreases, and the data is stored in the new SP
location.
• POP: Retrieve data from the stack. The Stack Pointer (SP) increases, and data is fetched from the SP location.
Visualization of Stack Operations:
○ PUSH: SP moves from 0x085F → 0x085E, stores data at 0x085E.
○ POP: SP moves back to 0x085F, retrieves data from 0x085E.
Sir, I understand you're looking for clarification on the CALL instructions and their relation to memory
addressing, as well as the concepts behind RCALL, ICALL, and EICALL in AVR assembly. Let's go through
these instructions one by one and connect them to memory addressing, including how the Z-register
works in ICALL and EICALL.
Sir, let's break down the details of the Unconditional Branch (JMP) instruction in AVR assembly,
including the bit structure and how the address is calculated.
css
Copy code
[15:0] Opcode for JMP
[21:0] Target address
• The 16-bit opcode defines the JMP instruction itself.
• The target address is specified using 22 bits. This gives the instruction the ability to jump
anywhere in the program memory within the 22-bit address space.
asm
Copy code
Summary of JMP:
• Total instruction size: 32 bits (16 bits for opcode + 22 bits for target address).
• Address range: Can jump to any address within 4M words (or 8MB of memory).
• Cycle count: Typically 3 cycles.
• Absolute addressing: Directly specifies the target address in program memory.
Sir, let's go over the RJMP (Relative Jump) and IJMP (Indirect Jump) instructions in AVR assembly. These
are variations of jump instructions, each serving a different purpose when it comes to addressing memory
locations. I'll explain their operation, addressing methods, and bit structure.
scss
Copy code
[15:12] Opcode for RJMP
asm
Copy code
RJMP +100 ; Jump 100 instructions forward
RJMP -50 ; Jump 50 instructions backward
In the first case, it jumps 100 instructions forward from the current location. In the second case, it jumps
50 instructions backward.
csharp
Copy code
[15:0] Opcode for IJMP
• There are no bits for the address itself since the address is stored in the Z-register.
• The Z-register is a 16-bit register made up of R30 (low byte) and R31 (high byte).
How IJMP Works:
• The Z-register contains the absolute target address.
• The IJMP instruction reads this address and jumps to it.
Example:
asm
Copy code
LDI R30, LOW(0x1F00) ; Load the low byte of the address
LDI R31, HIGH(0x1F00) ; Load the high byte of the address
IJMP ; Jump to the address in Z-register (0x1F00)
Address Range:
Since the Z-register is 16 bits wide, IJMP can address the full 64K (65,536 words) of program memory.
Cycle Count:
• 2 cycles: One cycle to fetch the instruction and one to jump to the address stored in the Z-register.
Summary:
• RJMP is useful for short relative jumps within a small range of the current program counter.
• IJMP is used for indirect jumps where the address is stored in a register (Z-register).
• EIJMP extends this to larger memory spaces by using the EIND register for more significant bits.
Let me know if this clarifies the topic or if you need more examples!
3. Register File
• Size: 32 General-Purpose Registers (R0-R31)
• Purpose: Used by the CPU for arithmetic, logic operations, and general processing.
• Access: Fast access by the CPU.
Memory Map:
• Addresses: 0x0000 to 0x001F
Each register is 8 bits wide, and there are 32 such registers. They are named R0 to R31 and are used for
intermediate calculations and data storage during operations.
7. Stack Memory
• Size: Variable, allocated within SRAM.
• Purpose: Used for storing return addresses when a subroutine is called and for saving temporary
data during interrupts or subroutine execution.
Memory Map:
• The stack grows from higher memory addresses to lower memory addresses (descending stack).
The location of the stack pointer (SP) is user-configurable and is generally initialized at the end of
the SRAM space and moves downwards as data is pushed onto the stack.
• The Stack Pointer (SP) is a 16-bit register and points to the current location in the SRAM where
the next value will be pushed or popped.
Sir, in AVR Assembly (and assembly language in general), DEF, EQU, and SET are used to define
constants or symbolic names for values, but they have slightly different usages and behaviors. Here’s an
explanation of each:
assembly
Copy code
.DEF myRegister = R16
This tells the assembler that whenever you write myRegister, it should treat it as R16. The value of
myRegister is fixed and can only be a register.
Example:
assembly
Copy code
.DEF tempReg = R16 ; Define a symbolic name for register R16
2. EQU (Equate)
• Purpose: Used to define a constant. The value associated with the name is fixed and cannot be
changed once defined. It is used to assign names to constants, such as addresses, bit positions, or
values that will be used throughout the program.
• Usage: It is a symbolic constant and is typically used for defining constants or memory addresses.
Syntax:
assembly
Copy code
.EQU myConstant = 0x20
This means that myConstant will always represent the value 0x20. It can’t be modified later in the
program.
Example:
assembly
Copy code
.EQU ledPin = 0x01 ; Define a constant value for the LED pin (bit 0)
.EQU portAddress = 0x25 ; Define a constant for the address of the I/O port
LDI R16, ledPin ; Load the LED pin value (0x01) into R16
OUT portAddress, R16 ; Write the value of R16 to the I/O port (0x25)
In this example, ledPin is a constant with a value of 0x01, and portAddress refers to an I/O address
(0x25).
3. SET
• Purpose: Similar to EQU, but it is not immutable. Once defined, the value associated with the
symbol can be changed later in the program. It's like a variable that can be reassigned multiple
times during assembly.
• Usage: Used when you want a symbolic name to represent a value that can change during
assembly (e.g., recalculating values during the assembly process).
Syntax:
assembly
Copy code
.SET myVariable = 0x10
This assigns the value 0x10 to myVariable, but unlike EQU, you can change the value later.
Example:
assembly
Copy code
.SET counterValue = 0x10 ; Set the initial value of the counter
LDI R16, counterValue ; Load counterValue (0x10) into R16
.SET counterValue = 0x20 ; Update the value of counterValue
LDI R17, counterValue ; Load the updated value (0x20) into R17
In this case, counterValue is first set to 0x10, but then later it is updated to 0x20.
Key Differences:
Feature DEF EQU SET
MEMORIZE
assembly
Copy code
SBI Register_Address, Bit_Number
○ Register_Address: The address of the I/O register where you want to set a bit.
○ Bit_Number: The bit position (0 to 7) you want to set within that register.
• Example:
assembly
Copy code
SBI PORTB, 2
This instruction sets the 2nd bit (bit 2) of PORTB to 1, making PB2 high.
• Explanation:
If PORTB = 00000000, after executing the above SBI command, it will become 00000100 (binary),
where PB2 is set to 1.
• Cycle Time:
The SBI instruction takes 2 clock cycles to execute.
assembly
Copy code
assembly
Copy code
CBI PORTB, 2
This instruction clears the 2nd bit (bit 2) of PORTB, making PB2 low.
• Explanation:
If PORTB = 00000100, after executing the above CBI command, it will become 00000000 (binary),
where PB2 is cleared to 0.
• Cycle Time:
The CBI instruction also takes 2 clock cycles to execute.
Visualizing SBI/CBI:
Let’s say PORTB = 00001100 (binary):
• SBI PORTB, 0:
PORTB becomes 00001101 (sets bit 0).
• CBI PORTB, 3:
PORTB becomes 00000100 (clears bit 3).
assembly
Copy code
SBIC Register_Address, Bit_Number
Register_Address: The address of the I/O register.
assembly
Copy code
SBIC PINB, 0
In this example, the instruction checks if bit 0 of the PINB register is cleared (0). If it is cleared, the
next instruction is skipped.
• Explanation:
Let's say PINB = 00000000, i.e., PB0 is 0 (cleared). The SBIC instruction will skip the next
instruction.
If PINB = 00000001, i.e., PB0 is 1, the next instruction will be executed normally.
• Cycle Time:
○ If the bit is cleared (skip occurs), it takes 1 additional clock cycle to skip the next instruction.
○ If the bit is set, it takes 1 clock cycle (the next instruction is executed).
assembly
Copy code
SBIS Register_Address, Bit_Number
○ Register_Address: The address of the I/O register.
○ Bit_Number: The bit position (0 to 7) to check.
• Example:
assembly
Copy code
SBIS PINB, 0
In this example, the instruction checks if bit 0 of the PINB register is set (1). If it is set, the next
instruction is skipped.
• Explanation:
Let's say PINB = 00000001, i.e., PB0 is 1 (set). The SBIS instruction will skip the next instruction.
If PINB = 00000000, i.e., PB0 is 0, the next instruction will be executed normally.
• Cycle Time:
○ If the bit is set (skip occurs), it takes 1 additional clock cycle to skip the next instruction.
○ If the bit is cleared, it takes 1 clock cycle (the next instruction is executed).
assembly
Copy code
ROR Rd ; Rotate right the contents of Rd through the carry flag
• Example:
assembly
Copy code
R16 = 10010110
Carry = 1
assembly
Copy code
ROL Rd ; Rotate left the contents of Rd through the carry flag
• Example:
assembly
Copy code
LSL Rd ; Logical shift left the contents of Rd
• Example:
assembly
Copy code
R16 = 10010110
assembly
Copy code
LSR Rd ; Logical shift right the contents of Rd
• Example:
assembly
Copy code
R16 = 10010110
assembly
assembly
Copy code
R16 = 10010110
assembly
Copy code
SWAP Rd ; Swap the nibbles of Rd
• Example:
assembly
Copy code
R16 = 10010110 ; Upper nibble = 1001, Lower nibble = 0110
Summary of Operations:
Instruction Operation Example Before Example After
ROR Rotate right through carry 10010110 (C=1) 11001011 (C=0)
ROL Rotate left through carry 10010110 (C=1) 00101101 (C=1)
LSL Shift left by 1 (logical) 10010110 00101100 (C=1)
LSR Shift right by 1 (logical) 10010110 01001011 (C=0)
ASR Shift right by 1 (arithmetic) 10010110 11001011 (C=0)
SWAP Swap nibbles 10010110 01101001
These instructions are useful for manipulating bit-level data, and they are often used in applications like
signal processing, encryption, and compression algorithms.
ChatGPT said:
ChatGPT
Let's break down the problem and write the assembly code step by step for the ATmega32 to achieve
the following:
1. Monitor Pin PB2 (bit 2 of PORT B) until it becomes high.
• This can be done by checking the input pin register (PINB) in a loop.
• We keep looping until PB2 becomes high.
2. When PB2 becomes high:
• Write $45 to PORTC.
• Send a high-to-low pulse to Pin PD3 (bit 3 of PORT D).
assembly
Copy code
SUB Rd, Rr
Example:
assembly
Copy code
LDI R16, 5 ; Load R16 with 5
LDI R17, 3 ; Load R17 with 3
SUB R16, R17 ; Subtract R17 from R16 (R16 = R16 - R17, R16 = 2)
• After the execution of the SUB instruction, R16 will contain 2 (because 5 - 3 = 2).
Status Register (SREG) flags affected by SUB:
• Carry (C): Set if there is a borrow from the result.
• Zero (Z): Set if the result is zero.
• Negative (N): Set if the result is negative (sign bit is 1).
• Overflow (V): Set if there is a signed overflow in the result.
• Sign (S): Combined result of N and V.
assembly
Copy code
SBC Rd, Rr
Example (Multi-byte subtraction):
assembly
Copy code
LDI R16, 0x01 ; Low byte
LDI R17, 0x00 ; High byte
LDI R18, 0x02 ; Low byte to subtract
LDI R19, 0x00 ; High byte to subtract
SUB R16, R18 ; Subtract low byte
SBC R17, R19 ; Subtract high byte with carry if needed
• After the execution:
○ SUB subtracts R18 from R16 (low byte), and if there’s a borrow (carry), it will affect the SBC
instruction.
SBC subtracts R19 and takes into account the carry.
assembly
Copy code
SUBI Rd, K
Example:
assembly
Copy code
LDI R16, 10 ; Load R16 with 10
SUBI R16, 5 ; Subtract 5 from R16 (R16 = 10 - 5 = 5)
• After execution, R16 will hold the value 5.
assembly
Copy code
SBIW Rd+1, K
• Rd: The low byte of the register pair (R0-R1, R2-R3, ..., R30-R31).
• Rd+1: The high byte of the register pair (the register following the low byte).
• K: The immediate value to subtract (0 to 63).
Example:
Let's say you want to subtract 5 from a 16-bit register pair:
assembly
Copy code
LDI R24, 0x00 ; Load the high byte with 0
LDI R25, 0x0A ; Load the low byte with 10 (0x0A)
SBIW R24, 5 ; Subtract 5 from R25:R24 (10 - 5 = 5)
• After execution:
○ R24 will contain 0x00 (high byte).
○ R25 will contain 0x05 (low byte), resulting in a 16-bit value of 0x0005.
assembly
Copy code
LDI R16, 48 ; Load the value 48 into register R16 (binary: 0011 0000)
; Rotate right three times to divide by 8
ROR R16 ; First ROR (48 becomes 0001 1000)
ROR R16 ; Second ROR (24 becomes 0000 1100)
ROR R16 ; Third ROR (12 becomes 0000 0110)
; R16 now contains the result of 48 / 8, which is 6
Bit-by-Bit Breakdown
Let's see the result after each ROR operation:
1. Initial Value (48):
○ R16: 0011 0000 (48 in decimal)
2. After First ROR:
○ R16: 0001 1000 (24 in decimal)
3. After Second ROR:
○ R16: 0000 1100 (12 in decimal)
4. After Third ROR:
○ R16: 0000 0110 (6 in decimal)
Final Result
After performing three ROR instructions, the value in R16 will be 6, which is the result of dividing 48 by
8.