Hello World - Spring 2007

Homework 8

Attempt by: Monday, April 9
Due by: Thursday, April 12

You may work with a partner on this assignment if you wish.

Reading

Simulator

The assembly language simulator is available here. On a Mac, download this file to your Desktop, double-click it, and then choose "Assembler" in the pop-up window. On Windows, you'll need to have Java installed, which is available from Sun here (download the Java Runtime Environment/JRE 6u1).

Problems To Hand In

  1. Write an assembly language program to add up the numbers from 1 to N. Your program should get the value of N from the user and output the final sum (in binary). Here is a pseudocode outline to use as your starting point:

    input a value N
    set SUM to 0
    while N >= 1 do         (if N = 0 quit the loop)
       set SUM to SUM+N
       set N to N-1
    (end of loop)
    output SUM
    

    After writing out your program on paper, type the code into the simulator and save it with the filename addup.asm. Then try it out to make sure it works. Here are some test cases to use:

    input:  11 (N = 3)
    output: 110 (sum = 6)
    
    input:  10000 (N = 16)
    output: 10001000 (sum = 136)
    
    input:  11111111 (N = 255)
    output: 111111110000000 (sum = 32,640)
    

    If you don't get the above answers, then something is wrong with your code. To debug it, try executing it one step at a time on very simple input (such as the N=3 case above), using the Step button instead of Run. After each step, carefully examine all of the registers and memory locations to verify that they got updated exactly as you expected.

  2. Write an assembly language program to divide a number by 2 using repeated subtraction. Your program should get the number from the user and then output both the quotient and the remainder. Save your code with the filename div2.asm. Here is a pseudocode outline of the algorithm:

    input a value N
    set QUOT to 0
    set REM to N
    while REM >= 2 do       (if REM < 2 quit the loop)
       set REM to REM-2
       set QUOT to QUOT+1
    (end of loop)
    output QUOT
    output REM              (0 means N is even, 1 means N is odd)
    

    Test cases:

    input                               output
    N = 10 (2)              QUOT = 1                REM = 0
    N = 11 (3)              QUOT = 1                REM = 1
    N = 1101 (13)           QUOT = 110 (6)          REM = 1
    N = 1110 (14)           QUOT = 111 (7)          REM = 0
    N = 100000000 (256)     QUOT = 10000000 (128)   REM = 0
    N = 11110111 (247)      QUOT = 1111011 (123)    REM = 1
    
  3. Write an assembly language program to compute the "3n+1" sequence. Your program should get a number N greater than 1 from the user and then output a sequence of numbers according to the rule: if N is even, divide N by 2; if N is odd, triple it and add 1; repeat until N becomes 1. Save your code with the filename sequence.asm. Here is a pseudocode outline of the algorithm:

    input a value N     (must be greater than 1)
    while N > 1 do      (if N = 1 then quit the loop)
       output N
       set QUOT to the quotient of N/2
       set REM to the remainder of N/2
       if REM = 0 (that is, if N is even) then
          set N to QUOT
       else
          set N to 3N+1
    (end of loop)
    output N
    

    You can use your code from the previous problem to compute the values of QUOT and REM in the above algorithm. Here are some test cases to try:

    input: 1
    output:
    0000000000000001
    
    input: 100 (N = 4)
    output:
    0000000000000100
    0000000000000010
    0000000000000001
    
    input: 11 (N = 3)
    output:
    0000000000000011
    0000000000001010
    0000000000000101
    0000000000010000
    0000000000001000
    0000000000000100
    0000000000000010
    0000000000000001
    
    input: 111 (N = 7)
    output:
    0000000000000111
    0000000000010110
    0000000000001011
    0000000000100010
    0000000000010001
    0000000000110100
    0000000000011010
    0000000000001101
    0000000000101000
    0000000000010100
    0000000000001010
    0000000000000101
    0000000000010000
    0000000000001000
    0000000000000100
    0000000000000010
    0000000000000001
    
  4. Write an assembly language program that repeatedly gets positive numbers from the user until a zero is entered, then prints out the biggest number entered. Save your code with the filename max.asm. Here is a pseudocode outline:

    set MAX to 0
    repeat:
       input a value N
       if N = 0 then
          quit the loop
       else if N > MAX then
          set MAX to N
    (end of loop)
    output MAX
    

    Test cases:

    inputs: 10, 100, 101, 11, 1, 0
    output: 101
    
    inputs: 111, 0
    output: 111
    
    inputs: 10000, 1111, 11, 0
    output: 10000
    

Turning in Your Homework

For this homework, you will submit both hardcopy and electronic versions of your programs. I'll use the electronic versions for testing purposes. IMPORTANT: If you work with a partner, please only submit one copy of your assignment, but make sure to put both of your names on your printout so that you both receive credit.

Print out your files addup.asm, div2.asm, sequence.asm, and max.asm and turn these in during class.

Go to the homework upload web page, choose the course Hello World, and log in using your Sarah Lawrence email address as your username, including the @slc.edu extension. The password is the username minus the @slc.edu extension. For example, I would log in as jmarshall@slc.edu with the password jmarshall. The first thing you should do is change your password. After that, follow the steps below to submit your files:

  1. Click on Upload Files for an Assignment and select Homework 8 (currently the only available choice).

  2. Next, select the files to submit one by one, using the Browse button. Each file is added to the list of files to submit. If you accidentally select a wrong file, just click Delete next to the file name.

  3. Once all files have been selected (addup.asm, div2.asm, sequence.asm, and max.asm), click on Upload Files Listed Below to submit them. The system should send you a confirmation email. Do not delete this email! Keep it as your receipt. If you do not receive such an email after a few minutes, then the submission did not succeed. In this case, try it again or let me know that you're having problems.

If you have questions about anything, just ask!