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?

Systems Architecture
7th Edition
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Stephen D. Burd
Chapter3: Data Representation
Section: Chapter Questions
Problem 10RQ: What primitive data types can normally be represented and processed by a CPU?
icon
Related questions
Topic Video
Question

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?

Expert Solution
Step 1

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

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning