Memory holds one byte per address, so a 32-bit word takes four addresses. Show where each byte lands on a little-endian machine.
What to build
A program that stores 32-bit words in consecutive memory starting at a base address and prints every byte with its address.
Requirements
- Word number j is stored at addresses base + 4j to base + 4j + 3, least significant byte first.
- Print one line per byte, in increasing address order, as
address: XXwith the address in decimal and the byte as two uppercase hex digits.
Input
A line with the base address, a line with n, then n lines, each a 32-bit word as 8 hex digits.
Output
4 × n lines.
Constraints
1 ≤ n ≤ 20; the base address is a whole number from 0 to 1000000.
Example
Input:
100
1
12345678
Output:
100: 78
101: 56
102: 34
103: 12
Why it works
A little-endian machine writes the low byte of the number first. Shifting right by 8 × i bits and masking with 0xFF isolates byte i, and the words follow each other every four addresses.
Examples
100 1 12345678
100: 78 101: 56 102: 34 103: 12
0 2 000000FF A1B2C3D4
0: FF 1: 00 2: 00 3: 00 4: D4 5: C3 6: B2 7: A1
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Take byte i of a word with (word >> (8 * i)) & 0xFF.
Hint 2
Little-endian means byte 0 (the least significant) goes to the lowest address.
Hint 3
Print a byte as two uppercase hex digits with f"{b:02X}".
Solution
Show one correct solution (try it yourself first)
base = int(input())
n = int(input())
for j in range(n):
word = int(input().strip(), 16)
for i in range(4):
b = (word >> (8 * i)) & 0xFF
print(f"{base + 4 * j + i}: {b:02X}")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step