Write an assembly language program that corresponds to the following C program: int num; int main () { scanf("%d", &num); num = num / 16; printf("num = %d\n", num); return 0; } please what is the assembly code for this?
Write an assembly language
int num;
int main () {
scanf("%d", &num);
num = num / 16;
printf("num = %d\n", num);
return 0;
}
please what is the assembly code for this?
Answer:
Here is an example of an x86 assembly language program that corresponds to the given C program:
section .data
num dd 0
section .bss
input resd 1
section .text
global _start
_start:
; Read integer into num
mov eax, 3 ; system call number for scanf
mov ebx, 0 ; file descriptor 0 (stdin)
mov ecx, input ; address of buffer to hold input
mov edx, 4 ; size of buffer
int 0x80 ; call kernel
; Store integer value from buffer into num
mov eax, [input]
mov [num], eax
; Divide num by 16
mov eax, [num]
mov edx, 0 ; clear high word
mov ebx, 16 ; divisor
div ebx
mov [num], eax
; Print num
mov eax, 4 ; system call number for printf
mov ebx, 1 ; file descriptor 1 (stdout)
mov ecx, num ; address of string to print
mov edx, 4 ; size of string
int 0x80 ; call kernel
; Exit program
mov eax, 1 ; system call number for exit
xor ebx, ebx ; exit status 0
int 0x80 ; call kernel
Trending now
This is a popular solution!
Step by step
Solved in 2 steps