Difference between revisions of "CSC231 Lab 1 2014"

From dftwiki3
Jump to: navigation, search
(Assembly and Linking steps)
(Submit Your Solution to Moodle)
 
(41 intermediate revisions by the same user not shown)
Line 1: Line 1:
This lab is a first lab to get you started with the process of assembling, linking, and running assembly programs for the Intel processor.  
+
<meta name="keywords" content="computer science, assembly language, pentium, exercise, machine language, intel" />
 +
<meta name="description" content="Dominique Thiebaut's Web Page" />
 +
<meta name="title" content="Dominique Thiebaut -- Computer Science" />
 +
<meta name="abstract" content="Dominique Thiebaut's Computer Science Web pages" />
 +
<meta name="author" content="thiebaut at cs.smith.edu" />
 +
<meta name="distribution" content="Global" />
 +
<meta name="revisit-after" content="10 days" />
 +
<meta name="copyright" content="(c) D. Thiebaut 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,2008" />
 +
<meta name="robots" content="FOLLOW,INDEX" />
 +
--[[User:Thiebaut|D. Thiebaut]] 19:55, 7 September 2010 (UTC)
 +
----
  
 +
 
__TOC__
 
__TOC__
 +
<br />
 +
<br />
 +
<tanbox>
 +
This lab is the first lab of this course and its purpose is to bring everybody up to speed with the assembly process, and have a tiny bit of fun at the same time.  '''It will be graded'''.  '''You have to submit it to Moodle before 9/11/14, 9:00 a.m'''. for it to be automatically evaluated and graded.
 +
</tanbox>
  
 +
<br />
 +
<br />
 +
 +
 +
=Work Due=
 +
<br />
 +
* Solution for Program 1, submitted on Moodle
 +
* Solution for Program 2, submitted on Moodle
 +
* Due date: 9/11/14, 9:00 a.m.
 +
<br />
 +
 +
=Computer Accounts=
 +
<br />
 +
* You should get a new computer account for CSC231.  You will need to
 +
*# record your new account and password in a safe place,
 +
*# read the [http://cs.smith.edu/dftwiki/index.php/Policy policy for the acceptable use of your new account]
 +
*# sign the piece of paper given to you
 +
*# return it to me
 +
<br />
 +
=SSH to Beowulf2=
 +
<br />
 +
* To connect to the new server for CS classes:
 +
 +
  ssh -Y 231a-'''xx'''@beowulf2.csc.smith.edu
 +
 +
:(replace '''xx''' by your new 2-letter Id)
 +
<br />
 
=Creating a program from scratch=
 
=Creating a program from scratch=
 +
<br />
 +
* Find the skeleton program on the class Web page (or open it [[CSC231_skeleton_program | here]])
 +
 +
* Login to beowulf with your new 231a-xx account (see the [[CSC231_Lab_1#Troubleshooting |'''Troubleshooting''']] section at bottom of this page in case of trouble)
 +
 +
* Create a file called '''skel.asm''' in your account, and save the [[CSC231 skel.asm | skeleton program]] in it.  At the Linux prompt, type:
  
* Find the skeleton program on the class Web page
+
  emacs -nw skel.asm
  
* Login to beowulf with your new 231a-xx account
+
:If you do not remember emacs commands, use the [[Emacs Quick Reference| Emacs Quick-Reference]] page for the most often used commands.
  
* Create a file called skel.asm in your account, and save the skeleton program in it.
+
* Save it by typing '''Control-X''' followed by '''Control-C''' in emacs. You should be back at the Linux prompt.
  
 
* Make a copy of [[CSC231_skel.asm | skel.asm]] and call it '''lab1.asm'''
 
* Make a copy of [[CSC231_skel.asm | skel.asm]] and call it '''lab1.asm'''
 +
 +
  cp skel.asm lab1.asm
 +
 +
* Edit lab1.asm
 +
 +
  emacs -nw lab1.asm
  
 
* Add a string variable in your data section:
 
* Add a string variable in your data section:
Line 20: Line 75:
 
                
 
                
  
* Add some code to output the string in the ''text (code) section''
+
* Add some code to output the string in the ''text (code) section'', right below the '''_start:''' label
  
 
                
 
                
               mov    eax, WRITE
+
               mov    eax, 4
               mov    ebx, STDOUT
+
               mov    ebx, 1
 
               mov    ecx, msg      ; use the same name as the string in the data section
 
               mov    ecx, msg      ; use the same name as the string in the data section
 
               mov    edx, MSGLEN  ; # of chars in string
 
               mov    edx, MSGLEN  ; # of chars in string
 +
              int    0x80
  
 
                
 
                
  
* Save your file
+
* Save your file by typing '''Control-X''', '''Control-C''' while in emacs.
 +
 
  
 
==Assembly and Linking steps==
 
==Assembly and Linking steps==
  
* Assemble your program
+
* You should be back at the Linux prompt.  Assemble your program
 
                
 
                
              nasm -f elf -F stabs lab1.asm
+
    nasm -f elf -F stabs lab1.asm
 
                
 
                
  
* Fix any errors you may get
+
* Fix any errors you may get by starting emacs again and figuring out where the error is.
  
 
* Link your program
 
* Link your program
  
              ld -o lab1 lab1.o
+
    ld -melf_i386  -o lab1 lab1.o
  
You shouldn't get any errors at this stage.
+
:You shouldn't get any errors at this stage.
  
 
==Execution==
 
==Execution==
Line 51: Line 108:
 
* Run your program:
 
* Run your program:
  
              ./lab1
+
    ./lab1
 
                
 
                
 +
:It should run and display the string. 
 +
 +
==Playing around with the program==
 +
 +
* Modify the msg string to make it look like this:
 +
 +
              msg    db    10, 10, "Welcome", 10, "to", 10, "csc231", 10, 10
 +
              MSGLEN  equ    $-msg
 +
 +
* Reassemble, link and run the new version of your program.
 +
 +
* Something different.  Modify your program once more, as shown below:
  
==Playing around with the program
+
              msg    db    10, 10
 +
                      db    "Welcome", 10
 +
                      db    "to", 10, "csc231"
 +
                      db    10
 +
                      db    10
 +
              MSGLEN  equ    $-msg
  
* Make your program output look like this:
 
  
                    Welcome to CSC111
+
* Reassemble, link and run the new version of your program.
                    Home of the Assembly Language
 
  
 
                
 
                
  
* If your first solution uses two strings, modify your program so that it uses only 1 string.  If your first solution uses one string, modify your program so that it uses two.
+
{| style="width:100%; background:silver"
 +
|-
 +
|
  
* Modify your program so that its output looks like this:
+
== Problem #1 ==
 +
|}
 +
[[Image:QuestionMark3.jpg|right|120px]]
  
                    *********************************
+
* Copy the previous program in a new file, called '''lab1_1.asm'''
                    * Welcome to CSC111            *
 
                    * Home of the Assembly Language *
 
                    *********************************
 
  
 +
* Modify your new program so that its '''output''' looks like this:
 +
 +
*********************************
 +
*&nbsp;Welcome&nbsp;to&nbsp;CSC231&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*
 +
*&nbsp;Home&nbsp;of&nbsp;the&nbsp;Assembly&nbsp;Language&nbsp;*
 +
*********************************
 
                
 
                
  
The lines of stars contain 33 *-characters. Each line is thus 34-character long (to account for the line-feed character). Can you find a solution where the number of characters in your data section is not 4 x 34 = 136 characters, but 3 x 34 = 102, instead?
+
The lines of stars contain 33 *-characters. Each line is thus 34-character long (to account for the line-feed character).
 +
 
 +
* Assemble and link your program.  Make sure it displays the correct output.
 +
 
 +
===Version 2===
 +
 
 +
* Can you alter your program so that the number of characters in your data section is not 4 x 34 = 136 characters, but 3 x 34 = 102 characters, instead? The reason for such a question is that often assembly language will be used by programmers to ''trim'' programs, i.e. making them faster or shorter so that their performance can be increased, or their footprint in memory can be lowered.
 +
 
 +
* Test it out!
 +
 
 +
<br />
 +
===Submit Your Solution to Moodle===
 +
<br />
 +
* Follow the steps illustrated in this [[Submitting programs for automatic testing on Moodle| page]] to submit your program to Moodle.
 +
<br />
 +
<br />
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
 
 +
== Problem #2 ==
 +
|}
 +
[[Image:QuestionMark4.jpg|right|120px]]
 +
 
 +
 
 +
* Write another program that will print out this series of strings on the screen:
 +
 
 +
*
 +
**
 +
***
 +
****
 +
*****
 +
 
 +
* Assemble, link, and run your program.  Make sure it works before continuing.
 +
* Count the number of *-characters that your program contains.  Is it 15?  If so, good; that's a good solution.  Here is a challenge for you: could you rewrite your program so that it contains only 5 *-characters?l
 +
 
 +
 
 +
<br />
 +
===Submit Your Solution to Moodle===
 +
<br />
 +
* Follow the steps illustrated in this [[Submitting programs for automatic testing on Moodle| page]] to submit your program to Moodle.  Select the '''Lab 1 Problem 2''' link on the Moodle Page.
 +
* The due date for both programs is 9/11/14 at 9:00 a.m. 
 +
<br />
 +
<br />
 +
 
 +
=Troubleshooting=
 +
 
 +
It is possible that if you are using '''ssh''' to connect to Beowulf the first time, and if you are using a Mac, you get a message of this form:
 +
 
 +
ssh 231a-xx@beowulf.csc.smith.edu
 +
==================================================
 +
    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!   
 +
==================================================
 +
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
 +
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
 +
It is also possible that the RSA host key has just been changed.
 +
The fingerprint for the RSA key sent by the remote host is
 +
bf:db:14:e6:a4:e0:f3:3d:d8:87:35:66:a9:35:68:fb.
 +
Please contact your system administrator.
 +
Add correct host key in /Users/alex/.ssh/known_hosts to get rid of this message.
 +
Offending key in /Users/xxxxx/.ssh/known_hosts:1
 +
RSA host key for beowulf.csc.smith.edu has changed and you have requested strict checking.
 +
Host key verification failed.
 +
 
 +
In this case, use the editor of your choice and edit the file '''~/.ssh/known_hosts''' and remove the lines that contains the word '''beowulf'''.  Be careful, the lines are very long and wrap around to form blocks of 4 or 5 lines on your screen, so deleting one line will require a big block to disappear.
 +
'''Bold text'''
 +
 
 +
<br />
 +
 
 +
<br />
 +
 
 +
<br />
 +
 
 +
<br />
 +
 
 +
<br />
 +
 
 +
<br />
  
Test it out!
+
<br />
 +
[[Category:CSC231]]

Latest revision as of 05:57, 4 September 2014

<meta name="keywords" content="computer science, assembly language, pentium, exercise, machine language, intel" /> <meta name="description" content="Dominique Thiebaut's Web Page" /> <meta name="title" content="Dominique Thiebaut -- Computer Science" /> <meta name="abstract" content="Dominique Thiebaut's Computer Science Web pages" /> <meta name="author" content="thiebaut at cs.smith.edu" /> <meta name="distribution" content="Global" /> <meta name="revisit-after" content="10 days" /> <meta name="copyright" content="(c) D. Thiebaut 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,2008" /> <meta name="robots" content="FOLLOW,INDEX" /> --D. Thiebaut 19:55, 7 September 2010 (UTC)





This lab is the first lab of this course and its purpose is to bring everybody up to speed with the assembly process, and have a tiny bit of fun at the same time. It will be graded. You have to submit it to Moodle before 9/11/14, 9:00 a.m. for it to be automatically evaluated and graded.




Work Due


  • Solution for Program 1, submitted on Moodle
  • Solution for Program 2, submitted on Moodle
  • Due date: 9/11/14, 9:00 a.m.


Computer Accounts



SSH to Beowulf2


  • To connect to the new server for CS classes:
 ssh -Y 231a-xx@beowulf2.csc.smith.edu

(replace xx by your new 2-letter Id)


Creating a program from scratch


  • Find the skeleton program on the class Web page (or open it here)
  • Login to beowulf with your new 231a-xx account (see the Troubleshooting section at bottom of this page in case of trouble)
  • Create a file called skel.asm in your account, and save the skeleton program in it. At the Linux prompt, type:
  emacs -nw skel.asm
If you do not remember emacs commands, use the Emacs Quick-Reference page for the most often used commands.
  • Save it by typing Control-X followed by Control-C in emacs. You should be back at the Linux prompt.
  • Make a copy of skel.asm and call it lab1.asm
  cp skel.asm lab1.asm
  • Edit lab1.asm
  emacs -nw lab1.asm
  • Add a string variable in your data section:
             msg     db     "Welcome to csc231", 10
             MSGLEN  equ    $-msg


  • Add some code to output the string in the text (code) section, right below the _start: label


             mov    eax, 4
             mov    ebx, 1
             mov    ecx, msg      ; use the same name as the string in the data section
             mov    edx, MSGLEN   ; # of chars in string
             int    0x80


  • Save your file by typing Control-X, Control-C while in emacs.


Assembly and Linking steps

  • You should be back at the Linux prompt. Assemble your program
    nasm -f elf -F stabs lab1.asm
             
  • Fix any errors you may get by starting emacs again and figuring out where the error is.
  • Link your program
    ld -melf_i386  -o lab1 lab1.o
You shouldn't get any errors at this stage.

Execution

  • Run your program:
   ./lab1
             
It should run and display the string.

Playing around with the program

  • Modify the msg string to make it look like this:
             msg     db     10, 10, "Welcome", 10, "to", 10, "csc231", 10, 10
             MSGLEN  equ    $-msg
  • Reassemble, link and run the new version of your program.
  • Something different. Modify your program once more, as shown below:
             msg     db     10, 10
                     db     "Welcome", 10
                     db     "to", 10, "csc231"
                     db     10
                     db     10
             MSGLEN  equ    $-msg


  • Reassemble, link and run the new version of your program.


Problem #1

QuestionMark3.jpg
  • Copy the previous program in a new file, called lab1_1.asm
  • Modify your new program so that its output looks like this:
*********************************
* Welcome to CSC231             *
* Home of the Assembly Language *
*********************************
             

The lines of stars contain 33 *-characters. Each line is thus 34-character long (to account for the line-feed character).

  • Assemble and link your program. Make sure it displays the correct output.

Version 2

  • Can you alter your program so that the number of characters in your data section is not 4 x 34 = 136 characters, but 3 x 34 = 102 characters, instead? The reason for such a question is that often assembly language will be used by programmers to trim programs, i.e. making them faster or shorter so that their performance can be increased, or their footprint in memory can be lowered.
  • Test it out!


Submit Your Solution to Moodle


  • Follow the steps illustrated in this page to submit your program to Moodle.



Problem #2

QuestionMark4.jpg


  • Write another program that will print out this series of strings on the screen:
*
**
***
****
*****
  • Assemble, link, and run your program. Make sure it works before continuing.
  • Count the number of *-characters that your program contains. Is it 15? If so, good; that's a good solution. Here is a challenge for you: could you rewrite your program so that it contains only 5 *-characters?l



Submit Your Solution to Moodle


  • Follow the steps illustrated in this page to submit your program to Moodle. Select the Lab 1 Problem 2 link on the Moodle Page.
  • The due date for both programs is 9/11/14 at 9:00 a.m.



Troubleshooting

It is possible that if you are using ssh to connect to Beowulf the first time, and if you are using a Mac, you get a message of this form:

ssh 231a-xx@beowulf.csc.smith.edu
==================================================
   WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     
==================================================
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
bf:db:14:e6:a4:e0:f3:3d:d8:87:35:66:a9:35:68:fb.
Please contact your system administrator.
Add correct host key in /Users/alex/.ssh/known_hosts to get rid of this message. 
Offending key in /Users/xxxxx/.ssh/known_hosts:1
RSA host key for beowulf.csc.smith.edu has changed and you have requested strict checking. 
Host key verification failed.

In this case, use the editor of your choice and edit the file ~/.ssh/known_hosts and remove the lines that contains the word beowulf. Be careful, the lines are very long and wrap around to form blocks of 4 or 5 lines on your screen, so deleting one line will require a big block to disappear. Bold text