0% found this document useful (0 votes)
8 views8 pages

MPL Assignment 3

The document provides an overview of data types in x86-64 assembly, including their sizes and corresponding registers. It explains the use of the cmp instruction for comparisons and conditional jumps based on CPU flags. Additionally, it outlines algorithms for finding maximum and minimum values in arrays, along with examples of array access and advanced logic for sorting and handling signed comparisons.

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)
8 views8 pages

MPL Assignment 3

The document provides an overview of data types in x86-64 assembly, including their sizes and corresponding registers. It explains the use of the cmp instruction for comparisons and conditional jumps based on CPU flags. Additionally, it outlines algorithms for finding maximum and minimum values in arrays, along with examples of array access and advanced logic for sorting and handling signed comparisons.

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/ 8

‭ASSIGNMENT 3‬

‭1. Data Types in x86-64‬

‭In‬‭x86-64‬‭assembly,‬‭data‬‭types‬‭define‬‭how‬‭much‬‭memory‬‭is‬‭allocated‬‭and‬‭how‬‭the‬
‭CPU interprets the data:‬

‭Common Data Types and Registers‬

‭Type‬ ‭Size‬ ‭Register Used‬

‭Byte‬ ‭8-bit‬ ‭al, bl, cl, dl, etc.‬

‭Word‬ ‭16-bit‬ ‭ax, bx, cx, dx, etc.‬

‭Dword‬ ‭32-bit‬ ‭eax, ebx, ecx, edx, etc.‬

‭Qword‬ ‭64-bit‬ ‭rax, rbx, rcx, rdx, etc.‬

‭Data Type Directives‬

‭●‬ ‭db: Define Byte (8 bits)‬

‭●‬ ‭dw: Define Word (16 bits)‬

‭●‬ ‭dd: Define Doubleword (32 bits)‬

‭●‬ ‭dq: Define Quadword (64 bits)‬

‭Examples‬

‭mov al, [array] ; Load a byte‬


‭mov ax, [array] ; Load a word (16 bits)‬
‭mov eax, [array] ; Load a dword (32 bits)‬
‭mov rax, [array] ; Load a qword (64 bits)‬

‭2. Comparison in Assembly‬


‭The‬ ‭cmp‬ ‭instruction‬‭compares‬‭two‬‭values‬‭by‬‭performing‬‭subtraction‬‭without‬‭storing‬
‭the result, but it‬‭sets CPU flags‬‭:‬

‭cmp rax, rbx‬

‭Affected Flags:‬

‭●‬ ‭ZF (Zero Flag):‬‭Set if equal‬

‭●‬ ‭SF (Sign Flag):‬‭Set if result is negative‬

‭●‬ ‭OF (Overflow Flag):‬‭Set if signed overflow‬

‭●‬ ‭CF (Carry Flag):‬‭Set if unsigned borrow occurs‬

‭Conditional Jump Instructions‬

‭Instruction‬ ‭Condition‬

‭je / jz‬ ‭Jump if equal (ZF = 1)‬

‭jne / jnz‬ ‭Jump if not equal (ZF = 0)‬

‭jg‬ ‭Jump‬ ‭if‬ ‭greater‬ ‭(ZF‬ ‭=‬ ‭0,‬ ‭SF‬ ‭=‬


‭OF)‬

‭jl‬ ‭Jump if less (SF ≠ OF)‬

‭jge‬ ‭Jump if ≥ (SF = OF)‬

‭jle‬ ‭Jump if ≤ (ZF = 1 or SF ≠ OF)‬

‭ja‬ ‭Jump if above (unsigned)‬

‭jb‬ ‭Jump if below (unsigned)‬

‭3. Finding Maximum in an Array‬

‭Algorithm:‬
‭1.‬ ‭Initialize max with the first element‬

‭2.‬ ‭Iterate through the rest‬

‭3.‬ ‭Compare each with max‬

‭4.‬ ‭Update max if needed‬

‭Sample Code:‬

‭mov rax, [rsi] ; max = first element‬


‭mov rdx, 1 ; index = 1‬

‭find_max_loop:‬
‭cmp rdx, rcx‬
‭jge find_max_done‬

‭cmp rax, [rsi + rdx*8]‬


‭jge skip_update‬

‭mov rax, [rsi + rdx*8]‬

‭skip_update:‬
‭inc rdx‬
‭jmp find_max_loop‬

‭find_max_done:‬
‭; rax contains max‬

‭4. Registers and Sizes‬

‭Byte (8-bit)‬

‭mov al, [array+index]‬


‭cmp al, bl‬
‭Word (16-bit)‬

‭mov ax, [array+index*2]‬


‭cmp ax, bx‬

‭Dword (32-bit)‬

‭mov eax, [array+index*4]‬


‭cmp eax, ebx‬

‭Qword (64-bit)‬

‭mov rax, [array+index*8]‬


‭cmp rax, rbx‬

‭Note:‬‭The scaling factor (*1, *2, *4, *8) matches the data size.‬

‭Viva Questions and Answers‬

‭Basics‬

‭Q1. What are the sizes of Byte, Word, Dword, Qword?‬

‭●‬ ‭Byte = 8 bits (1 byte)‬

‭●‬ ‭Word = 16 bits (2 bytes)‬

‭●‬ ‭Dword = 32 bits (4 bytes)‬

‭●‬ ‭Qword = 64 bits (8 bytes)‬

‭Q2. What are the general-purpose registers for each data size?‬

‭●‬ ‭8-bit:‬‭al, bl, cl, dl, ah, etc.‬

‭●‬ ‭16-bit:‬‭ax, bx, cx, dx, si, di, etc.‬


‭●‬ ‭32-bit:‬‭eax, ebx, ecx, edx, esi, etc.‬

‭●‬ ‭64-bit:‬‭rax, rbx, ..., r8 to r15‬

‭Q3. What does cmp do?‬


‭It‬ ‭compares‬ ‭two‬ ‭operands‬ ‭by‬ ‭subtracting‬ ‭them‬ ‭and‬ ‭setting‬ ‭CPU‬ ‭flags‬ ‭(ZF,‬ ‭SF,‬ ‭OF,‬
‭CF), used by jump instructions.‬

‭Intermediate‬

‭Q1. How do conditional jumps work?‬

‭cmp rax, rbx‬


‭jg greater ; if rax > rbx‬
‭jmp continue‬

‭greater:‬
‭; Do something‬

‭continue:‬

‭Q2. What flags does cmp set?‬

‭●‬ ‭ZF, SF, OF, CF‬‭depending on result of subtraction.‬

‭Q3. How to use arrays with various element sizes?‬

‭Element Type‬ ‭Instruction Example‬

‭Byte‬ ‭mov al, [array + index]‬

‭Word‬ ‭mov ax, [array + index*2]‬

‭Dword‬ ‭mov‬ ‭eax,‬ ‭[array‬ ‭+‬


‭index*4]‬

‭Qword‬ ‭mov rax, [array + index*8]‬


‭Advanced‬

‭Q1. How does overflow affect signed comparisons?‬


‭Signed‬‭overflow‬‭may‬‭cause‬‭incorrect‬‭result.‬‭Flags‬‭SF‬‭and‬‭OF‬‭are‬‭both‬‭used‬‭to‬‭check‬
‭signed conditions.‬

‭Example:‬

‭cmp rax, rbx‬


‭jg label ; uses ZF = 0 and SF = OF‬

‭Q2. Can we find min and max in one pass?‬

‭mov rax, [rsi] ; max‬


‭mov rbx, [rsi] ; min‬
‭mov rdx, 1‬

‭minmax_loop:‬
‭cmp rdx, rcx‬
‭jge done‬

‭cmp rax, [rsi + rdx*8]‬


‭jge check_min‬
‭mov rax, [rsi + rdx*8]‬

‭check_min:‬
‭cmp rbx, [rsi + rdx*8]‬
‭jle next‬
‭mov rbx, [rsi + rdx*8]‬

‭next:‬
‭inc rdx‬
‭jmp minmax_loop‬

‭done:‬
‭; rax = max, rbx = min‬
‭Q3. How to sort an array (bubble sort)?‬

‭mov r8, rcx‬


‭dec r8‬

‭outer_loop:‬
‭test r8, r8‬
‭jz done‬

‭mov r9, 0‬

‭inner_loop:‬
‭cmp r9, r8‬
‭jge next_outer‬

‭mov rax, [rsi + r9*8]‬


‭mov rbx, [rsi + r9*8 + 8]‬
‭cmp rax, rbx‬
‭jle no_swap‬

‭mov [rsi + r9*8], rbx‬


‭mov [rsi + r9*8 + 8], rax‬

‭no_swap:‬
‭inc r9‬
‭jmp inner_loop‬

‭next_outer:‬
‭dec r8‬
‭jmp outer_loop‬

‭done:‬
‭; Sorted array‬

‭Summary‬
‭●‬ ‭Data Types‬‭: Byte to Qword, each with specific registers and scaling.‬

‭●‬ ‭Comparison‬‭: Done using cmp, result used by conditional jumps.‬

‭●‬ ‭Maximum/Minimum‬‭: Use loops with comparisons and updates.‬

‭●‬ ‭Array Access‬‭: Scaling in addressing must match element size.‬

‭●‬ ‭Advanced‬ ‭Logic‬‭:‬ ‭Sorting,‬ ‭min-max‬ ‭in‬ ‭one‬ ‭pass,‬ ‭signed‬ ‭vs.‬ ‭unsigned‬
‭comparison handling.‬

You might also like