MP Assignment1
MP Assignment1
What is assembly language? Assembly language is a low-level programming
language that provides a symbolic representation of machine code instructions
specific to a particular computer architecture. It uses mnemonics (like MOV,ADD,
JMP) to represent machine instructions, making it easier for humans to read and
write compared to raw binary machine code.
Whatisanassembler?Anassemblerisaprogramthattranslatesassemblylanguage
codeintomachinecode(objectcode)thatcanbeexecuteddirectlybytheCPU.
It converts the human-readable mnemonics into binary instructions that the
processor understands.
High-level languages(likePython,Java,C++)aremoreabstractedfromhardware,
use English-like syntax, are portable across different architectures, and focus on
solving problems rather than hardware details.
Low-level languages (like assembly) are closely tied to specific hardware
architecture, provide direct access to hardware resources, have a one-to-one
correspondence with machine instructions, and require deeper understanding of
computer architecture.
Registers in x86-64:
rax: Accumulator register, used for arithmetic operations and function return values
rbx: Base register, often used as a base pointer for memory access
Other registers: rbp (base pointer), rsp (stack pointer), r8 through r15 (general
purpose)
Purpose of 64-bit registers: 64-bit registers allow for handling larger integer values
and addressingmorememory.Theycanholdvaluesupto2^64-1andcandirectly
address up to 2^64 bytes (16 exabytes) of memory, though practical limitations
usually apply.
First 6 integer/pointer arguments pass in registers: rdi, rsi, rdx, rcx, r8, r9
How arrays are declared in assembly: Arrays are typically declared in the data
section using directives like:
How to store and access elements: Elements are accessed using base+index
addressing:
Indexed: mov rax, [rbx + rcx*4] - use base registerplus scaled index
Whatdoes64-bitmean?64-bitreferstothewidthofregistersandmemoryaddresses
in a processor architecture. It means the CPU can process 64 bits of data in one
operation and can directly address 2^64 bytes of memory.
Howmanyhexadecimaldigitsfitin64bits?Sinceeachhexdigitrepresents4bits,64
bits can be represented using 16 hexadecimal digits (64 ÷ 4 = 16).
Unsigned:All64bitsrepresentthemagnitude,allowingvaluesfrom0to2^64-1(0to
18,446,744,073,709,551,615)
Signed: Most significant bit indicates sign (0 for positive, 1 for negative), allowing
values from -2^63 to 2^63-1 (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
cmp a, b - compare values and set flags (like zero, carry, overflow)
Usually followed by conditional jumps like je (jump if equal), jl (jump if less),
etc.
Basic
1. What is assembly language and why do we use it? Assembly language is a
low-level programming language that provides a symbolic representation of
machine code. We use it when we need direct hardware control, maximum
performance, or when writing code for specific hardware features not accessible
from high-level languages.
2. What is the size of a 64-bit hexadecimal number? A 64-bit number can be
represented using 16 hexadecimal digits, since each hex digit represents 4 bits.
3. How do you define an array in assembly? Arrays in assembly are defined by
allocating consecutive memory locations:
4.Whatarethegeneral-purposeregistersinx86-64?Thegeneral-purposeregistersin
x86-64 include: rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp, and r8 through r15.
Medium
1.Howdoyouconvertahexadecimalstringtoanumber?Toconvertahexstringto
a number:
For each character: a. Convert character to its numeric value (0-15) b. Multiply
current result by 16 c. Add the numeric value
Continue until all characters are processed
2. How do you take input in assembly? In Linux x86-64, you use the read syscall
(syscall number 0):
3. How is a number displayed on the screen in x86-64 assembly? To display a
number, you typically:
cmp: Compares two values and sets CPU flags based on result
Advanced
Code Specific
1.Howdidyoustoretheinputvalues?Inputvaluesaretypicallystoredinabufferin
the data section, then parsed and converted to appropriate internalformat(e.g.,
binary representation in registers or memory).
2.Howdidyouensuretheinputwas64-bit?Byvalidatingtheinputlength(ensuringit
doesn't exceed 16 hex digits) and using 64-bit registers/memory locations to store
the converted values.
3. What syscall numbers did you use for I/O (if using Linux)?
4. How did you handle the loop for storing/displaying? Typically using a counter
register (like rcx) and a pointer register (like rsi or rdi) to track position, with
conditional jumps or the loop instruction to repeat the process for each element.