0% found this document useful (0 votes)
7 views5 pages

Prac4

The document outlines a practical assignment for writing an X86/64 assembly language program that performs 64-bit hexadecimal arithmetic operations (addition, subtraction, multiplication, and division) using a switch-case structure. It includes a detailed program with macros for input/output operations, procedures for each arithmetic operation, and functions to convert between ASCII strings and integers. The program prompts the user for input, processes the arithmetic based on the user's choice, and displays the results.

Uploaded by

Paras Kamble
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)
7 views5 pages

Prac4

The document outlines a practical assignment for writing an X86/64 assembly language program that performs 64-bit hexadecimal arithmetic operations (addition, subtraction, multiplication, and division) using a switch-case structure. It includes a detailed program with macros for input/output operations, procedures for each arithmetic operation, and functions to convert between ASCII strings and integers. The program prompts the user for input, processes the arithmetic based on the user's choice, and displays the results.

Uploaded by

Paras Kamble
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/ 5

PRACTICAL NO.

04
Name:- Swapnil
Suryavanshi
Roll No:- S213074
Div:- C
Batch:- C2

Problem Statement:-
Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic
operations (+,-,*, /) using suitable macros. Define procedure for each operation.

Program:-
%macro IO 4
mov rax, %1
mov rdi, %2
mov rsi, %3
mov rdx, %4
syscall
%endmacro

section .data
menu db "Enter Choice (+, -, *, /): ",10
menu_len equ $ - menu

msg1 db "Enter first number: ",10


msg1_len equ $ - msg1

msg2 db "Enter second number: ",10


msg2_len equ $ - msg2

msg_add db "Addition Result: ",0


msg_sub db "Subtraction Result: ",0
msg_mul db "Multiplication Result: ",0
msg_div_q db "Quotient: ",0
msg_div_r db "Remainder: ",0

newline db 10
invalid db "Invalid Choice!",10
invalid_len equ $ - invalid

section .bss
choice resb 2
num1 resb 20
num2 resb 20
result resb 20

section .text
global _start
_start:
; Show menu and get user choice
IO 1, 1, menu, menu_len
IO 0, 0, choice, 2

; Get first number


IO 1, 1, msg1, msg1_len
IO 0, 0, num1, 20

; Get second number


IO 1, 1, msg2, msg2_len
IO 0, 0, num2, 20

; Convert input to numbers


mov rsi, num1
call str_to_int
mov r8, rax ; store first number in r8

mov rsi, num2


call str_to_int
mov r9, rax ; store second number in r9

; Perform operation based on choice


cmp byte [choice], '+'
je do_add
cmp byte [choice], '-'
je do_sub
cmp byte [choice], '*'
je do_mul
cmp byte [choice], '/'
je do_div

; Invalid choice
IO 1, 1, invalid, invalid_len
jmp exit

do_add:
mov rax, r8
add rax, r9
mov rsi, result
call int_to_str
IO 1, 1, msg_add, 18
IO 1, 1, result, 20
IO 1, 1, newline, 1
jmp exit

do_sub:
mov rax, r8
sub rax, r9
mov rsi, result
call int_to_str
IO 1, 1, msg_sub, 20
IO 1, 1, result, 20
IO 1, 1, newline, 1
jmp exit

do_mul:
mov rax, r8
imul rax, r9
mov rsi, result
call int_to_str
IO 1, 1, msg_mul, 23
IO 1, 1, result, 20
IO 1, 1, newline, 1
jmp exit

do_div:
mov rax, r8
xor rdx, rdx
mov rbx, r9
div rbx ; rax = quotient, rdx = remainder

; print remainder
mov rsi, result
mov rax, rdx
call int_to_str
IO 1, 1, msg_div_r, 10
IO 1, 1, result, 20
IO 1, 1, newline, 1

; print quotient
mov rsi, result
mov rax, r8
xor rdx, rdx
div r9
call int_to_str
IO 1, 1, msg_div_q, 9
IO 1, 1, result, 20
IO 1, 1, newline, 1

jmp exit

exit:
mov rax, 60
xor rdi, rdi
syscall

;
; Convert ASCII string to int
; rsi -> address of string
; returns result in rax
str_to_int:
xor rax, rax
xor rcx, rcx
.next_digit:
mov bl, [rsi + rcx]
cmp bl, 10
je .done
cmp bl, 0
je .done
cmp bl, '0'
jb .done
cmp bl, '9'
ja .done
sub bl, '0'
imul rax, rax, 10
add rax, rbx
inc rcx
jmp .next_digit
.done:
ret

;
; Convert int in rax to ASCII string
; rsi -> output buffer
; rax -> input number
int_to_str:
mov rcx, 0
mov rbx, 10
.next:
xor rdx, rdx
div rbx
add dl, '0'
push rdx
inc rcx
test rax, rax
jnz .next

; pop to buffer
.rev:
pop rax
mov [rsi], al
inc rsi
loop .rev
mov byte [rsi], 0
ret
Output:-

You might also like