0% found this document useful (0 votes)
3 views7 pages

MP Assignment1

The document provides an overview of assembly language, detailing its definition, the role of assemblers, and the differences between high-level and low-level programming languages. It covers x86-64 architecture, including register functions, memory handling, input/output operations, data types, and control flow mechanisms. Additionally, it addresses common questions related to assembly language and its practical applications.

Uploaded by

9iraj.jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

MP Assignment1

The document provides an overview of assembly language, detailing its definition, the role of assemblers, and the differences between high-level and low-level programming languages. It covers x86-64 architecture, including register functions, memory handling, input/output operations, data types, and control flow mechanisms. Additionally, it addresses common questions related to assembly language and its practical applications.

Uploaded by

9iraj.jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

‭ASSIGNMENT 1‬

‭1. Basic Assembly Language Concepts‬

‭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.‬

‭What‬‭is‬‭an‬‭assembler?‬‭An‬‭assembler‬‭is‬‭a‬‭program‬‭that‬‭translates‬‭assembly‬‭language‬
‭code‬‭into‬‭machine‬‭code‬‭(object‬‭code)‬‭that‬‭can‬‭be‬‭executed‬‭directly‬‭by‬‭the‬‭CPU.‬
‭It‬ ‭converts‬ ‭the‬ ‭human-readable‬ ‭mnemonics‬ ‭into‬ ‭binary‬ ‭instructions‬ ‭that‬ ‭the‬
‭processor understands.‬

‭Difference between high-level and low-level languages:‬

‭High-level‬ ‭languages‬‭(like‬‭Python,‬‭Java,‬‭C++)‬‭are‬‭more‬‭abstracted‬‭from‬‭hardware,‬
‭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.‬

‭2. x86-64 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‬

‭rcx: Counter register, used in loop and string operations‬

‭rdx: Data register, used in I/O operations and extended multiplication/division‬

‭rsi: Source index, source pointer for string operations‬


‭rdi: Destination index, destination pointer for string operations‬

‭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‬ ‭addressing‬‭more‬‭memory.‬‭They‬‭can‬‭hold‬‭values‬‭up‬‭to‬‭2^64-1‬‭and‬‭can‬‭directly‬
‭address‬ ‭up‬ ‭to‬ ‭2^64‬ ‭bytes‬ ‭(16‬ ‭exabytes)‬ ‭of‬ ‭memory,‬ ‭though‬ ‭practical‬ ‭limitations‬
‭usually apply.‬

‭Calling conventions (System V for Linux):‬‭System V‬‭AMD64 ABI specifies:‬

‭First 6 integer/pointer arguments pass in registers: rdi, rsi, rdx, rcx, r8, r9‬

‭Additional arguments pass on the stack‬

‭Return values in rax (and rdx for larger values)‬

‭Caller-saved registers: rax, rcx, rdx, r8-r11‬

‭Callee-saved registers: rbx, rbp, rsp, r12-r15‬

‭3. Memory and Arrays‬

‭How‬ ‭arrays‬ ‭are‬ ‭declared‬ ‭in‬ ‭assembly:‬ ‭Arrays‬ ‭are‬ ‭typically‬ ‭declared‬ ‭in‬ ‭the‬ ‭data‬
‭section using directives like:‬

‭array: dq 1, 2, 3, 4, 5 ; array of 5 quad-words (64-bit values)‬

‭Or as uninitialized space:‬

‭array: resq 10 ; reserve space for 10 quad-words‬

‭How‬ ‭to‬ ‭store‬ ‭and‬ ‭access‬ ‭elements:‬ ‭Elements‬ ‭are‬ ‭accessed‬ ‭using‬ ‭base+index‬
‭addressing:‬

‭mov rax, [array] ; load first element‬


‭mov rax, [array + 8] ; load second element (8 bytes per quad-word)‬
‭mov rax, [array + rcx*8] ; load element at index rcx‬
‭Addressing modes:‬

‭Direct‬‭: mov rax, [address] - access memory at a fixed‬‭address‬

‭Indirect‬‭: mov rax, [rbx] - access memory at address‬‭stored in register‬

‭Indexed‬‭: mov rax, [rbx + rcx*4] - use base register‬‭plus scaled index‬

‭4. Input and Output Handling‬

‭How to take user input:‬‭In Linux x86-64 using syscalls:‬

‭; Read from stdin‬


‭mov rax, 0 ; syscall number for read‬
‭mov rdi, 0 ; file descriptor 0 (stdin)‬
‭mov rsi, buffer ; buffer to store input‬
‭mov rdx, length ; number of bytes to read‬
‭syscall‬

‭How to display a number in hex format:‬‭To display‬‭in hex, you typically:‬

‭Extract each 4-bit nibble from the number‬

‭Convert each nibble to its ASCII character ('0'-'9', 'A'-'F')‬

‭Output the resulting string‬

‭Conversion routines:‬‭For hex string to number:‬

‭Read each character‬

‭Convert ASCII to its numeric value ('0'-'9' → 0-9, 'A'-'F' → 10-15)‬

‭Shift existing value left by 4 bits and add new digit‬

‭For number to hex string:‬

‭Extract 4 bits from the right‬

‭Convert to ASCII ('0'-'9', 'A'-'F')‬

‭Repeat for all nibbles‬


‭5. Data Types and Hex Numbers‬

‭What‬‭does‬‭64-bit‬‭mean?‬‭64-bit‬‭refers‬‭to‬‭the‬‭width‬‭of‬‭registers‬‭and‬‭memory‬‭addresses‬
‭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.‬

‭How‬‭many‬‭hexadecimal‬‭digits‬‭fit‬‭in‬‭64‬‭bits?‬‭Since‬‭each‬‭hex‬‭digit‬‭represents‬‭4‬‭bits,‬‭64‬
‭bits can be represented using 16 hexadecimal digits (64 ÷ 4 = 16).‬

‭Signed vs unsigned interpretation:‬

‭Unsigned‬‭:‬‭All‬‭64‬‭bits‬‭represent‬‭the‬‭magnitude,‬‭allowing‬‭values‬‭from‬‭0‬‭to‬‭2^64-1‬‭(0‬‭to‬
‭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)‬

‭6. Looping and Control Flow‬

‭Looping over an array:‬

‭mov rcx, array_length ; initialize counter‬


‭mov rsi, array ; pointer to array‬
‭loop_start:‬
‭; Process element at [rsi]‬

‭add rsi, 8 ; move to next element (8 bytes per quad-word)‬


‭dec rcx ; decrement counter‬
‭jnz loop_start ; jump if not zero‬

‭Alternative using loop:‬

‭mov rcx, array_length ; initialize counter‬


‭mov rsi, array ; pointer to array‬
‭loop_start:‬
‭; Process element at [rsi]‬

‭add rsi, 8 ; move to next element‬


‭loop loop_start ; decrement rcx and jump if not zero‬
‭How to use inc, dec, and cmp instructions:‬

‭inc reg - increment register by 1‬

‭dec reg - decrement register by 1‬

‭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.‬

‭Answers to Possible Viva Questions‬

‭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:‬

‭array: dq 1, 2, 3, 4, 5 ; initialized array‬


‭buffer: resq 100 ; uninitialized array of 100 quad-words‬

‭4.‬‭What‬‭are‬‭the‬‭general-purpose‬‭registers‬‭in‬‭x86-64?‬‭The‬‭general-purpose‬‭registers‬‭in‬
‭x86-64 include: rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp, and r8 through r15.‬

‭Medium‬

‭1.‬‭How‬‭do‬‭you‬‭convert‬‭a‬‭hexadecimal‬‭string‬‭to‬‭a‬‭number?‬‭To‬‭convert‬‭a‬‭hex‬‭string‬‭to‬
‭a number:‬

‭Start with result = 0‬

‭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):‬

‭mov rax, 0 ; syscall number for read‬


‭mov rdi, 0 ; file descriptor (stdin)‬
‭mov rsi, buffer ; address of buffer‬
‭mov rdx, length ; maximum bytes to read‬
‭syscall‬

‭3.‬ ‭How‬ ‭is‬ ‭a‬ ‭number‬ ‭displayed‬ ‭on‬ ‭the‬ ‭screen‬ ‭in‬ ‭x86-64‬ ‭assembly?‬ ‭To‬ ‭display‬ ‭a‬
‭number, you typically:‬

‭Convert it to a string representation (decimal, hex, etc.)‬

‭Use the write syscall to output the string:‬

‭mov rax, 1 ; syscall number for write‬


‭mov rdi, 1 ; file descriptor (stdout)‬
‭mov rsi, buffer ; address of string‬
‭mov rdx, length ; length of string‬
‭syscall‬

‭4. What is the purpose of mov, cmp, jmp?‬

‭mov: Copies data from source to destination‬

‭cmp: Compares two values and sets CPU flags based on result‬

‭jmp: Unconditional jump to a different part of the code‬

‭Advanced‬

‭1.‬ ‭Explain‬ ‭how‬ ‭the‬ ‭data‬ ‭is‬ ‭stored‬ ‭in‬ ‭the‬‭memory‬‭for‬‭64-bit‬‭numbers.‬‭64-bit‬‭numbers‬


‭occupy‬ ‭8‬ ‭consecutive‬ ‭bytes‬ ‭in‬ ‭memory.‬ ‭In‬ ‭x86-64‬ ‭(which‬ ‭is‬ ‭little-endian),‬ ‭the‬ ‭least‬
‭significant‬‭byte‬‭is‬‭stored‬‭at‬‭the‬‭lowest‬‭memory‬‭address,‬‭and‬‭the‬‭most‬‭significant‬‭byte‬
‭is at the highest of the 8 addresses.‬

‭2. What are the addressing modes in x86?‬‭Key addressing‬‭modes include:‬


‭Immediate: Using constant values (mov rax, 42)‬

‭Register: Using register contents (mov rax, rbx)‬

‭Direct: Using fixed memory address (mov rax, [address])‬

‭Register indirect: Using register as pointer (mov rax, [rbx])‬

‭Base+displacement: Using register plus offset (mov rax, [rbx+8])‬

‭Base+index+scale+displacement: (mov rax, [rbx+rcx*4+8])‬

‭3. Explain the difference between rax and eax.‬

‭rax is the full 64-bit register‬

‭eax is the lower 32 bits of rax‬

‭Writing to eax zeros the upper 32 bits of rax‬

‭ax is the lower 16 bits, al is the lowest 8 bits‬

‭Code Specific‬

‭1.‬‭How‬‭did‬‭you‬‭store‬‭the‬‭input‬‭values?‬‭Input‬‭values‬‭are‬‭typically‬‭stored‬‭in‬‭a‬‭buffer‬‭in‬
‭the‬ ‭data‬ ‭section,‬ ‭then‬ ‭parsed‬ ‭and‬ ‭converted‬ ‭to‬ ‭appropriate‬ ‭internal‬‭format‬‭(e.g.,‬
‭binary representation in registers or memory).‬

‭2.‬‭How‬‭did‬‭you‬‭ensure‬‭the‬‭input‬‭was‬‭64-bit?‬‭By‬‭validating‬‭the‬‭input‬‭length‬‭(ensuring‬‭it‬
‭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)?‬

‭Syscall 0 (read) for input‬

‭Syscall 1 (write) for output‬

‭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.‬

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy