Spaces provided and submit online



Download 21.26 Kb.
Date28.01.2017
Size21.26 Kb.
#9792
TECH3233

Spring 2015

Lab #3 - Intro to ASM
Name:_____________________________
This lab is due one week from today! Please type your answers in the spaces provided and submit online. Do not forget to include your NAME in the DOCUMENT.
Purpose:
To familiarize the student with creating an ASM program, compiling, linking and debugging on the Galileo Board.
Procedure:
First, insert the SD Card as show:








Now plug in the Power Supply and RS-232 cable as show:



Plug in the other end of the RS-232 cable (DB-9) to the back of a desktop computer.
Since the Galileo board is a computer in it’s own right, we will be communicating with the board via an RS-232 Terminal Emulator program called Putty. You can get this program from http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe. Since this program does not need to be installed, it can be placed directly on a thumb drive and executed from there.
Open the program and set it up as follows:

Depending on when you power up the board and connect to it via putty, you might see the Linux boot process taking place. The boot process does include a few menus and prompts, just wait for them to time out and continue. Then you should see:



The login is “root” (do not type quotes).
We are using the SD card to boot to boot to Yocto Linux Ver 3.8.7. Since we are dealing with a command line Linux Distribution, you will need to know some basic commands:


ls

list files in current directory

rm filename

delete file named filename

nano –w filename

create or edit the file name filename. the –w turns off wordwrap

Using nano, create a file called lab4.asm and type in the following asm code:


section .text

global _start

_start:

mov al,00000101b



mov bl,0Ah

mov cl,al

inc cl

mov dx, 0xa123



add al,bl

sub al,cl


mov eax,1

int 0x80
A note on the nano text editor.



  • In the menu at the bottom of the screen the “^” indicates you hold down the Ctrl key and the character that follows. so Ctrl + x exits the program (with the option to save if the file has been modified)

Once you have create the file, exit nano (as described above) making sure to save the file.


The ASM file is just a text representation of the program, but the process does not understand ASM, it understands the binary one’s and zero’s of MACHINE LANGUAGE which is NOT the same. To get the ASM program to Machine Language (executable code) the program must be compiled.
To compile the code, use the following instruction:
nasm –f elf lab4.asm
if you do not receive any errors (ie you just go right back to a prompt) then the program compiled and a new file called lab4.o has been created.
If you receive any compiler errors, go back and double check the file you created for errors.
There is one last step to make the program an executable file. We must link the program to libraries being used. A Linker is a computer program that takes one or more objects (the .o file) generated by a compiler and links them to standard library function and hence making them executable program1. We will us the following command to link:
ld –m elf_i386 –o lab4 lab4.o
This creates the executable file lab4 (no extension)
Since the program does not output anything to the screen, you will not see any output when you execute the program. but if you wanted to you could execute it by typing:
./lab4
The ./ is required, since linux only searches the path, not the current working directory for an executable file. By adding the ./ you are telling it to look at the current working directory.
Since the program produces no visible output, we are going to use a debugger to take a look at what the code does. A debugger or debugging tool is a computer program that allows a programmer to step though each assembly line instruction and see the effect on the internal processor registers, examine memory, and see the flag register after the execution of each individual assembly instruction.
We will be using a debugger call “Assembly Language Debugger” (ald). This is NOT the standard debugger for Linux (typically gdb is used, but it is way too complex for our purposes). To start the debugger type the following:
ald lab4
When you execute the line above, you should see:

Help is available via the ‘help’ command which will show you the various commands that can be run at this time. I have also created a pdf with the help and all the detailed help for each instruction. It can be found one the Galileo webpage off the class website.
We wish to step though the program (aka perform a trace) to see the effect of each line of code on the registers as they are executed. To do this we first have to set up a break point which tells ald to stop at a particular point in the program. We want to stop right at the beginning so we can use the command:
break _start
this tells ald to stop at the location in memory where our first instruction (labeled with _start: in our asm file) is located.

now type the command


run
which will execute the program from the beginning until it see the breakpoint we just set. The output will look as follows (some values might be different on your machine):

First it says it is running the program lab4. It reaches the breakpoint and stops the program, and shows all the registers BEFORE THE LINE IS EXECUTED then it shows the line that will be executed next.

The line of code to be executed next is broken down into parts. In the example above, the 08048060 is the memory location of the instruction. <_start> is the label we assigned to the start of our program in the ASM file. The B005, more rightly B0 05 is how the mov al, 0x5 is stored in memory in bytes. The B0 represents the mov into al a number immediately following the command) and 0x5 is the hex value being moved into the register al.
Before continuing, a note about registers. Our program uses AL, BL,CL, and DX but yet the above output does not show these registers, at least not directly. But look at the following diagram:

Data Registers2


This shows that the Register EAX, contains register AX (the lower 16 bits) and AX is made up of AH (upper byte) and AL (lower byte of AX). This same pattern is repeated for EBX, ECX and EDX.
To move on to the next instruction, type:
step
which will then execute the line it stopped at, show you the registers and flags (with the changes implemented by the command just executed) then show the next command about the be executed.
To continue on stepping though the program, just repeat the step command (HINT: you can use the up arrow on the keyboard to scroll though old commands, so if the last command you entered was step, up arrow and enter would repeat that command)
One last note: the last two instructions are used to terminate the program using a Linux Syscall (sys_exit). This will be explained further in future lectures. For now just except the last two lines terminate the program.
Answer the following questions:

  1. In general terms what does a MOV instruction do?




  1. Answer the following:

    1. How does the line “MOV BL, 0Ah” differ from “MOV DX,0x0A123”?


    1. What does the 0x, h and b indicate in the program?



    1. What do you suppose would happen if we change the MOV bl,0Ah to MOV bl,10?



    1. How many bits are stored by:

      1. MOV BL,0AH


      1. MOV DX,0x0A123


    1. What 8bit registers are effected by:

      1. MOV BL,0AH


      1. MOV DX,0x0A123



    1. What happens when the instruction MOV CL,AL is executed. What is in AL when the instruction is finished executing.


      1. What function does MOV really perform in the step?



  1. What does the add instruction do and in what register does the answer end up?




  1. Write the mathematical statement that the program performs using registers AL,BL and CL (excluding DX and the last two lines of code)




  1. What does the INC instruction do?



  1. Go back to the original window and edit the program so that ADD AL,BL is replaced with ADD BL,AL. Re compile and trace the program (excluding the last two lines of code), what changed?



  1. Compared to #4 above, how did the mathematical statement change?




  1. There should be an “FF” in AL when you do #6 above.

    1. What does this indicate?



    1. Also the S flag (SF in Flags) is now set (a one), what does this indicate?




  1. What does the EIP register do during each program step? How does this relate to the number of bytes in the MACHINE LANGUAGE instruction?



1 http://en.wikipedia.org/wiki/Linker

2 http://www.tutorialspoint.com/assembly_programming/assembly_registers.htm


Download 21.26 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page