Difference between revisions of "CSC231 Lab 5 2010"
(→Preparation For Homework 5) |
|||
Line 2: | Line 2: | ||
---- | ---- | ||
+ | --[[User:Thiebaut|D. Thiebaut]] 14:12, 22 October 2010 (UTC) | ||
+ | ---- | ||
+ | __TOC__ | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <tanbox> | ||
+ | This lab is in preparation for the current [[CSC231 Homework 5 2010 |homework assignment]] due 10/28. It shows you the steps necessary for mixing C and assembly programs together. | ||
+ | </tanbox> | ||
+ | <br /> | ||
+ | <br /> | ||
=Preparation For [[CSC231 Homework 5 2010 | Homework 5]]= | =Preparation For [[CSC231 Homework 5 2010 | Homework 5]]= | ||
<Font color="magenta">Important Note: this problem must be done on '''grendel.csc.smith.edu''', as it requires a 32-bit machine, while beowulf is a 64-bit machine. This has to do with mixing assembly (generated in 32-bit mode) and C-programs (compiled in 64-bit on beowulf, and in 32-bit on grendel).</Font> | <Font color="magenta">Important Note: this problem must be done on '''grendel.csc.smith.edu''', as it requires a 32-bit machine, while beowulf is a 64-bit machine. This has to do with mixing assembly (generated in 32-bit mode) and C-programs (compiled in 64-bit on beowulf, and in 32-bit on grendel).</Font> | ||
− | While printing strings of characters on the screen is relatively easy in assembly, as you saw in the previous assignment and exercises, inputing numbers and printing numbers is more difficult in assembly (we'll see why in class). | + | While printing strings of characters on the screen is relatively easy in assembly, as you saw in the previous assignment and exercises, inputing numbers and printing numbers is more difficult in assembly (we'll see why in class later). |
− | So, for the time being, to deal with the input/output of integers we will use a trick: we will link our assembly language program to a C program, and make the C program handle the input and output of information for us. | + | So, for the time being, to deal with the input/output of integers we will use a trick: '''we will link our assembly language program to a C program, and make the C program handle the input and output of information for us.''' |
Don't worry if you don't know C. We will not write a single line of C code except for the small C program which will never change. | Don't worry if you don't know C. We will not write a single line of C code except for the small C program which will never change. | ||
− | When we want to print a number from our assembly program, or when we want to get a number from the keyboard, we simply call the appropriate C function from our assembly program. Because the linker will link our program with the C program, we can call functions written in another language from assembly. This is a useful | + | When we want to print a number from our assembly program, or when we want to get a number from the keyboard, we simply call the appropriate C function from our assembly program. Because the linker will link our program with the C program, we can call functions written in another language from assembly. This is a useful programming trick! |
It sounds complicated, but you'll see that it isn't that bad. | It sounds complicated, but you'll see that it isn't that bad. | ||
− | + | ==Step 1== | |
− | First use your favorite editor to write the following C program and call it '''driver.c''' | + | * Let's try this concept with a simple example: |
+ | |||
+ | * First use your favorite editor to write the following C program and call it '''driver.c''' | ||
#include <stdio.h> | #include <stdio.h> | ||
Line 29: | Line 43: | ||
− | All it does is have a main program that calls a function '''asm_main()''', which will be the entry point of | + | * All it does is have a main program that calls a function '''asm_main()''', which will be the entry point of our assembly language program. |
− | |||
− | |||
− | * [[CSC231_asm_io.asm | asm_io.asm]] | + | * Next get a small library that will provide a simple interface between this C program and our assembly language program.<br /><br /> |
− | * [[CSC231_asm_io.inc | asm_io.inc]] | + | ** [[CSC231_asm_io.asm | asm_io.asm]]<br /><br /> |
+ | ** [[CSC231_asm_io.inc | asm_io.inc]] | ||
− | Create the object file for the library: | + | * Create the object file for the library: |
nasm -f elf -F stabs asm_io.asm | nasm -f elf -F stabs asm_io.asm | ||
− | Now enter the test program below in assembly. Call it '''hw5test.asm'''. Be careful that it is slightly different from the ones we have been writing so far in that''' the starting label and the last instructions are not the standard ones'''. | + | * Now enter the test program below in assembly. Call it '''hw5test.asm'''. Be careful that it is slightly different from the ones we have been writing so far in that''' the starting label and the last instructions are not the standard ones'''. |
;;; hw5test.asm | ;;; hw5test.asm |
Revision as of 10:12, 22 October 2010
--D. Thiebaut 23:12, 20 October 2010 (UTC)
--D. Thiebaut 14:12, 22 October 2010 (UTC)
Contents
This lab is in preparation for the current homework assignment due 10/28. It shows you the steps necessary for mixing C and assembly programs together.
Preparation For Homework 5
Important Note: this problem must be done on grendel.csc.smith.edu, as it requires a 32-bit machine, while beowulf is a 64-bit machine. This has to do with mixing assembly (generated in 32-bit mode) and C-programs (compiled in 64-bit on beowulf, and in 32-bit on grendel).
While printing strings of characters on the screen is relatively easy in assembly, as you saw in the previous assignment and exercises, inputing numbers and printing numbers is more difficult in assembly (we'll see why in class later).
So, for the time being, to deal with the input/output of integers we will use a trick: we will link our assembly language program to a C program, and make the C program handle the input and output of information for us.
Don't worry if you don't know C. We will not write a single line of C code except for the small C program which will never change.
When we want to print a number from our assembly program, or when we want to get a number from the keyboard, we simply call the appropriate C function from our assembly program. Because the linker will link our program with the C program, we can call functions written in another language from assembly. This is a useful programming trick!
It sounds complicated, but you'll see that it isn't that bad.
Step 1
- Let's try this concept with a simple example:
- First use your favorite editor to write the following C program and call it driver.c
#include <stdio.h> extern int asm_main( void ); int main() { asm_main(); }
- All it does is have a main program that calls a function asm_main(), which will be the entry point of our assembly language program.
- Next get a small library that will provide a simple interface between this C program and our assembly language program.
- Create the object file for the library:
nasm -f elf -F stabs asm_io.asm
- Now enter the test program below in assembly. Call it hw5test.asm. Be careful that it is slightly different from the ones we have been writing so far in that the starting label and the last instructions are not the standard ones.
;;; hw5test.asm ;;; a simple demo program to test I/O with ;;; a C "wrapper" program %include "asm_io.inc" ;; ------------------------- ;; data segment ;; ------------------------- section .data msg db "Hello! Please enter an integer number: ",0x00 msg2 db "You have entered ",0x00 x dd 0 ;; ------------------------- ;; code area ;; ------------------------- section .text global asm_main asm_main: mov eax,msg ; pass address of the string msg in eax call print_string ; call a function in asm_io library ; that will print the string msg call read_int ; call a function that reads an mov [x], eax ; integer from the keyboard and ; returns it in eax. Save returned ; value in variable x mov eax,msg2 ; print the string msg2 call print_string ; ("you have entered") mov eax,[x] ; get integer user entered call print_int ; pass it via eax and call the ; function specialized in printing ; integer numbers call print_nl ; call the function that prints ; a new blank line ;; return to C program ret
Note: the beginning of the program is not labeled _start any longer, but asm_main. The reason is that the start of the program is in the driver.c program, not in our assembly program. Also, the program does not end with an int 0x80 instruction any longer, but by a ret instruction, so that it can return to the C program which will end with its own (hidden) int 0x80.
Although we haven't seen the call instruction yet, it's fairly easy to understand what it does. It forces the processor to go somewhere else in the memory, execute a piece of code, and return back to the program. We'll look at it in details later. Right now the logic of the program should be sufficient for you to see how to use these simple functions to print strings and number.
Note: in C all strings of characters must be terminated by a 0 (0x00) byte. Because here we have used a C function to print the strings of characters, we added a 0x00 byte at the end of each string. Make sure that you add 0x00 at the end of all your strings of characters if you are going to use the method shown above to print your strings.
To assemble and run your program just follow these steps:
nasm -f elf -F stabs hw5test.asm gcc -o hw5test driver.c asm_io.o hw5test.o ./hw5test Hello! Please enter an integer number: 1234 You have entered 1234
The first line assembles hw5test.asm. The second line asks the C compiler gcc to compile driver.c and to link it with the object files asm_io.o and hw2test.o, and to store the executable in a file called hw5test. The last line executes the program.
The functions provided by the asm_io.o library are described now:
- read_int: reads an integer from the keyboard and stores it into the EAX register.
- print_int: prints out in 2's complement the value of the integer stored in EAX on the screen.
- print_string: prints out to the screen the contents of the string at the address stored in EAX. The string must be terminated by a 0x00 byte (C-string).
- print_char: prints out to the screen the character whose ASCII value is stored in AL
- read_char: reads a single character from the keyboard and stores its ASCII code into the EAX register.
- print_nl: prints out to the screen a new line character.
The asm_io library supports also two macros that you may find useful: dump_regs and dump_mem. They can be used to display the contents of the registers and the contents of memory. Try adding these two lines at the end of your hw5test.asm program:
call print_nl ; call the function that prints ; a new blank line dump_regs 1 dump_mem 1, msg, 3 ;; return to C program ret
The first of the new lines dump_regs 1 dumps the registers and uses 1 as a label, so that if you use this macro several times in your program, you can label each one with a different number so that you can figure out from the output which macro actually output what. The second of the new lines dump_mem 1, msg, 3 output 3 lines of 16 bytes starting at the address corresponding to the variable msg, and uses 1 as the label for this output.