diff --git a/README.md b/README.md index f3fece23a572e16f59b24331023d0ecf327ffe6c..a8cd2136873625c303290c76b530c1c9eac5a90b 100644 --- a/README.md +++ b/README.md @@ -91,3 +91,231 @@ For open source projects, say how it is licensed. ## Project status If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. + +from here this is the finally out put  + +this is the directory structure of the project. + +C-worksheet-01/ +│ +├── task1.asm # Assembly code for task 1 +├── task2.asm # Assembly code for task 2 +├── asm_io.asm # Assembly code for input/output handling +├── asm_io.inc # Include file for common I/O routines +├── driver.c # C driver code that integrates assembly routines +├── makefile # Makefile to build the project +├── README.md # Project documentation +└── task1.o # Object file for task 1 +└── task2.o # Object file for task 2 +└── task1 # Executable for task 1 +└── task2 # Executable for task 2 + +This will: + +Compile the C code (driver.c) into an object file. +Assemble the assembly code files (task1.asm, task2.asm, asm_io.asm). +Link the object files to create executables (task1, task2). + +Clean Build +To clean up the generated files (object files and executables), you can run: + + +Copy code +make clean +This will remove all .o files and executables. + +Running the Programs +After the build completes, you can run the task1 or task2 programs using the following commands: + +Copy code +./task2 + +Code Overview +task1.asm and task2.asm +These are the assembly files that define the main functionality for each task. They include: + +A prompt for user input (e.g., asking for a name). +System calls to handle input and output. +Functions like print_string and read_string that interact with the user. +asm_io.asm +This file contains low-level assembly routines for handling input and output. It includes the necessary system calls for reading from standard input and writing to standard output. + +driver.c +The C driver file calls the assembly routines by linking them together using function prototypes. It controls the flow of the program and integrates C and assembly code. + +makefile +The Makefile provides a set of rules to automate the build process, making it easier to compile and link the project. + +i change some code in makefile. + + +this is the output of the file, please have a look image. + + +Compilation Process +The project uses a Makefile for efficient compilation and linking. To build the project, run the following command: + +Copy code +make +This command compiles and assembles the necessary files and then links them into executable programs. + +Build Process Breakdown: +C Compilation: driver.c is compiled into an object file (driver.o). +Assembly Compilation: The assembly files (task1.asm, task2.asm, asm_io.asm) are assembled into object files. +Linking: All object files are linked together using gcc to produce the executables task1 and task2. +Clean Build +To remove all generated object files and executables, you can run the following command: + + +Copy code +make clean +This will clean up the project directory by removing the .o files and the executable files. + +Running the Programs +After a successful build, you can run the programs as follows: + +To run task1: + + + +Copy code +./task1 +To run task2: + + +Copy code +./task2 +Both programs prompt the user for input (such as a name) and display corresponding output messages. + +Code Walkthrough +task1.asm and task2.asm +These assembly files define the core functionality of the tasks. Here's an example of the assembly code in task2.asm: + +asm +Copy code +section .data + welcome_msg db "Welcome, ", 0 + name times 50 db 0 ; Reserve 50 bytes initialized to 0 + +section .text + extern print_string, read_string + global asm_main + +asm_main: + ; Prompt for name + push welcome_msg + call print_string + add esp, 4 + + ; Read the name input + push name + call read_string + add esp, 4 + + ; Print a thank you message + push thank_you_msg + call print_string + add esp, 4 + + ; Print the user's name + push name + call print_string + add esp, 4 + + ; Print a newline for better formatting + push newline + call print_string + add esp, 4 + + ; Exit the program + ret +In this snippet, the program does the following: + +Prompts the user with the message "Welcome, ". +Reads the user's name and stores it in the name buffer. +Prints a thank-you message and the user's name. +Finally, it prints a newline for proper formatting. +asm_io.asm +This file contains the routines for handling input and output. The function read_string reads a string from the user and stores it in a buffer. Here’s a snippet of the input handling code: + +asm +Copy code +read_string: + ; This function reads a string from the user and stores it in the buffer. + ; Input: ESP points to the address of the buffer. + ; Output: The buffer is filled with the user input. + + push ebp + mov ebp, esp + + ; Get the buffer address from the stack + mov eax, [ebp + 8] ; Address of the buffer is passed as the first argument + + ; Prepare to read input + mov ecx, eax ; Move the buffer address to ECX + mov edx, 50 ; Limit to 50 characters + call _read_input ; Call the helper function to read the string + + pop ebp + ret + +_read_input: + ; Reads input from the user and stores it in the buffer at ECX. + ; Limit is stored in EDX. + ; Uses Linux system call for reading input. + + mov eax, 3 ; sys_read syscall + mov ebx, 0 ; File descriptor 0 (stdin) + int 0x80 ; Make the syscall + ret +driver.c +The C driver code integrates the assembly functions. It defines the necessary prototypes for the assembly functions and invokes them during program execution. Here's a snippet: + +c +Copy code +extern void asm_main(); // Declare the assembly main function + +int main() { + asm_main(); // Call the assembly function to execute the task + return 0; +} +Makefile +The Makefile automates the compilation process. It includes rules to compile the C code, assemble the assembly files, and link the object files to generate the executables. Here's a simplified version of the Makefile: + +makefile +Copy code +all: task1 task2 + +task1: driver.o task1.o asm_io.o + gcc -m32 driver.o task1.o asm_io.o -o task1 + +task2: driver.o task2.o asm_io.o + gcc -m32 driver.o task2.o asm_io.o -o task2 + +driver.o: driver.c + gcc -m32 -c driver.c -o driver.o + +task1.o: task1.asm + nasm -f elf task1.asm -o task1.o + +task2.o: task2.asm + nasm -f elf task2.asm -o task2.o + +asm_io.o: asm_io.asm + nasm -f elf asm_io.asm -o asm_io.o + +clean: + rm -f *.o task1 task2 +Screenshots +Here’s a screenshot showing the successful compilation and execution of task2: + + +<!-- Replace with actual screenshot --> + +In the above terminal output, we can see that the make command successfully compiles and links the object files and the executables. After executing ./task2, the program prompts the user for input and displays the output. + +Conclusion +This project demonstrates integrating assembly and C to create programs that interact with the user via system calls for input/output. The assembly code handles user interaction, while the C code orchestrates the flow of the program, making it an excellent example of combining high-level and low-level programming. + +Feel free to fork the repository, contribute improvements, and submit issues or pull requests. + diff --git a/driver.o b/driver.o index aeedf97406b3d7a753c87bdfe70a6ab01aac9c93..865992f579a2ad3a9dc146e5fc8e6051b3302634 100644 Binary files a/driver.o and b/driver.o differ diff --git a/image-1.png b/image-1.png new file mode 100644 index 0000000000000000000000000000000000000000..cffe629768aab12d808828bb230a902fd71a024b Binary files /dev/null and b/image-1.png differ diff --git a/image-2.png b/image-2.png new file mode 100644 index 0000000000000000000000000000000000000000..cffe629768aab12d808828bb230a902fd71a024b Binary files /dev/null and b/image-2.png differ diff --git a/image.png b/image.png new file mode 100644 index 0000000000000000000000000000000000000000..a8ea528ab92f9957923b3771b818bde886d745f0 Binary files /dev/null and b/image.png differ diff --git a/makefile b/makefile index 7402b881384e7138458226fdc8d346bb6748b121..ca6ef5cc76a61593438e3158260ec591c3b76f3a 100644 --- a/makefile +++ b/makefile @@ -1,3 +1,22 @@ -hello: driver.c - gcc -o driver.o -c driver.c +all: task1 task2 +task1: driver.o task1.o asm_io.o + gcc -m32 driver.o task1.o asm_io.o -o task1 + +task2: driver.o task2.o asm_io.o + gcc -m32 driver.o task2.o asm_io.o -o task2 + +driver.o: driver.c + gcc -m32 -c driver.c -o driver.o + +task1.o: task1.asm + nasm -f elf task1.asm -o task1.o + +task2.o: task2.asm + nasm -f elf task2.asm -o task2.o + +asm_io.o: asm_io.asm + nasm -f elf asm_io.asm -o asm_io.o + +clean: + rm -f *.o task1 task2 diff --git a/task1 b/task1 new file mode 100755 index 0000000000000000000000000000000000000000..5d5ea7abf5d5a0b882afa327863efcb761ca74dd Binary files /dev/null and b/task1 differ diff --git a/task1.asm b/task1.asm new file mode 100644 index 0000000000000000000000000000000000000000..fdbbedfdf9168cd819ac4488a579f80668ce5fcb --- /dev/null +++ b/task1.asm @@ -0,0 +1,31 @@ +; Include the I/O macros from asm_io.inc +%include "asm_io.inc" + +segment .data + integer1 dd 17 ; Define the first 4-byte integer + integer2 dd 6 ; Define the second 4-byte integer + +segment .bss + result resd 1 ; Reserve space for the result (1 4-byte integer) + +segment .text + global asm_main ; Declare asm_main as a global symbol + +asm_main: + enter 0, 0 ; Setup stack frame + pusha ; Push all registers onto the stack + + ; Load integer1 into EAX register + mov eax, [integer1] ; Move the value at address integer1 to EAX + ; Add integer2 to EAX + add eax, [integer2] ; Add the value at address integer2 to EAX + ; Store the result back into the memory (result variable) + mov [result], eax ; Store the sum of integer1 and integer2 into result + + ; Display the result using print_int macro from asm_io.inc + call print_int ; Print the result stored in EAX + call print_nl + popa ; Restore registers + mov eax, 0 ; Return 0 in EAX (success status) + leave ; Clean up stack frame + ret ; Return control to the C program diff --git a/firstprogram.o b/task1.o similarity index 50% rename from firstprogram.o rename to task1.o index 491380fc3b60a124ec10459d1f29defec096ddcc..4a1bca6b8e7dc2440b69725426af26bbee206b57 100644 Binary files a/firstprogram.o and b/task1.o differ diff --git a/task2 b/task2 new file mode 100755 index 0000000000000000000000000000000000000000..316bc0f1469060a031092c05f3844e9aa13dd709 Binary files /dev/null and b/task2 differ