Implement binary search algorithm in AT&T syntax Assembly 64 bit. the code has to be only in x86-64 assembly
.model small ; assembler directive to allocate memory | |
.data ; data segment | |
arr dw 0000h, 1111h, 2222h, 3333h ; array of 10 16-bit numbers | |
dw 4444h, 5555h, 6666h | |
dw 7777h, 8888h, 9999h | |
len dw ($-arr)/2 ; length of the array | |
key equ 2222h ; key to be searched | |
msg1 db "key is found at " ; message 1 to print if key is found | |
res db " position ", 10, 13, "$" ; message 2 to print if key is found | |
msg2 db 'key is not found!!!.$' ; message to print if key is not found | |
.code ; code segment | |
mov ax, @data ; initialize ds register | |
mov ds, ax | |
mov bx, 00 ; bx = 0, lower bound | |
mov dx, len ; dx = len, upper bound | |
mov cx, key ; cx = key to be searched | |
again: ; label again | |
cmp bx, dx ; compare bx with dx | |
ja fail ; if bx > ax i.e. lower bound > upper bound, jump to label fail | |
mov ax, bx ; move bx i.e. lower bound to ax | |
add ax, dx ; add ax with dx, i.e. lower bound with upper bound and store result in ax | |
shr ax, 1 ; shift ax right by 1 to divide sum by 2 | |
mov si, ax ; move result in ax to si | |
add si, si ; add si with si to point to mid element | |
cmp cx, arr[si] ; compare cx with mid element | |
jae big ; if cx >= mid element, jump to label big else continue | |
dec ax ; decrement ax by one to get mid - 1 | |
mov dx, ax ; move dx to ax, change upper bound to mid - 1 | |
jmp again ; jump to label again | |
big: ; label big | |
je success ; if mid element = cx, jump to success else continue | |
inc ax ; increment ax by one to get mid + 1 | |
mov bx, ax ; move ax to bx, change lower bound to mid + 1 | |
jmp again ; jump to label again | |
success: ; label success | |
add al, 01 ; add 1 to al to obtain position of key in the array | |
add al, 30h ; add 30 to convert it to integer | |
lea si, res ; load effective address of res in si | |
mov [si], al ; replace character at first location of res with al, i.e. position | |
lea dx, msg1 ; load effective address of msg1 in dx | |
jmp disp ; jump to label disp | |
fail: ; label fail | |
lea dx, msg2 ; load effective address of msg2 in dx | |
disp: ; label disp | |
mov ah, 09h ; to print content present in dx | |
int 21h | |
mov ah, 4ch ; terminate with return code | |
int 21h | |
|
Implement binary search algorithm in AT&T syntax Assembly 64 bit. the code has to be only...
a) Write the following C function in Assembly. You must follow the System V 64-bit calling convention and use AT&T Syntax notation. Note: You cannot change the algorithm in any way so your assembly function must still be recursive. (20 points) long Catalan(long n) { long sum = 0; if (n == 0) return 1; for (int i = 0; i < n; i++) { sum += Catalan(i) * Catalan(n - i - 1); } return sum; } b) The...
a) Write the following C function in Assembly. You must follow the System V 64-bit calling convention and use AT&T Syntax notation. long fibonacci (long n) { if (n == 0) return 0; else if (n == 1) return 1; else return (fibonacci (n - 1) + fibonacci (n - 2)); } b) The Windows x86-64 calling convention passes function parameters in the registers RCX, RDX, R8 and R9 and returns values on register RAX. Caller saved registers are: RAX,...
pseudo code only no coding in necessary (a) Design a variant "binary" search algorithm which splits the set not into 2 sets of equal sizes (½ and ½), but into 2 sets of sizes one quarter (14) and three quarters (3/4) (b) Give the recurrence relations of your algorithm. (c) How does this algorithm compare with the original binary search in both the best case complexity and the worst case complexity?
2) Write a recursive procedure in pseudocode to implement the binary search algorithm. 3) Explain, how the binary search algorithm can be modified, or used, to insert, a new integer element x, into a sorted list of n intgers.
Implement the binary reflected Gray code algorithm for generating subsets in Java
1. Implement an algorithm to convert binary number into an integer. Binary number is represented as a string. Ex. int n = to_int("01010"); int to_int(const std::string& b) { } 2. Implement an algorithm to convert a decimal number to binary. The return type is string which holds the binary number as string. std::string to_binary(int n) { } 3. Implement a function to check if the number is positive or negative. The function should return true if number is positive. bool...
Describe an algorithm that checks if a given binary tree T is a binary search tree. Analyze your algorithm by giving its asymptotic runtime. Show your work.
C++ program 1. Construct a Binary Search Tree 50 Write code to implement a BST. Implement an add) method and a remove) method. Use the following input to construct a BST: 50,20,75,98,80,31,150, 39, 23,11,77 20 75 31 98 0 (150 Hint on removing If the node to be removed is: a leaf node a node with a left child only a node with a right child only a node with both children Then take this action replace with null replace...
Write an algorithm that will check if a tree is a binary search tree. If a tree is not a binary search tree, then it will repair it to make it one. (a) Write pseudo-code for both functions. (b) What is the running time for your algorithm?
Given a binary search tree and a value k, implement a function to find the node in the binary search tree whose value is closest to k. Write the program in Java Syntax: int lookup(Node node)