03.Lab02-2.LC-2K Assembly-Language Programming

更新时间:2023-12-01 08:26:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Laboratory2-2 Programming

LC-2KAssembly-Language

Laboratory2.2.1 Multiplication

1. Purpose

This project is intended to help you understand the instructions of a verysimple assembly language and how to write simple assembly language program.

2. Problem

In thislab, you will write a shortassembly-language program to multiply two numbers.

3. Assembly-Language Multiplication

This assignment is to write an assembly-language program tomultiply two numbers. Input the numbers by reading memory locations called\and \result should be stored in register 1 when theprogram halts. You may assume that the two input numbers are at most 15 bitsand are positive; this ensures that the (positive) result fits in an LC-2Kword. See the algorithm on page 178 of the textbook (3rd edition) or page232 of the textbook (4th edition) for an idea of how to multiply.

Remember that shifting left by one bit is the same as adding the number toitself. Given the LC-2K instruction set, it's easiest to modify thealgorithm so that you avoid the right shift. Submit a version of the programthat computes (32766 * 10383).

Your multiplication program must be reasonably efficient--it must be at most50 lines long and execute at most 1000 instructions for any valid input (thisis several times longer and slower than the solution). To achieve this, youmust use a loop and shift algorithm to perform the multiplication;

algorithmssuch as successive addition (e.g. multiplying 5 * 6 by adding 5 six times)will take too long.

4. Formatting

You must be careful to follow theexact formatting rules:

1) Store the result in register 1.

2) The two input numbers must be in locations labeled \

Laboratory2.2.2 Combination

1. Purpose

This project is intended to help you understand how procedure calls workin assembly language for a simple computer.

2. Problem

In this lab, you will write anassembly-language program to recursively compute combination(n,r)(i.e. n choose r).

3. Assembly-Language Program to Compute

Combination(n,r)

Write an assembly-language program to recursively computecombination(n,r), defined as:

combination(n,0) = 1 combination(n,n) = 1

combination(n,r) = combination(n-1,r) + combination(n-1,r-1);

0 <= r <= n

Here's a C function to do this; your assembly-language program will followthis logic closely.

int combination(int n, int r) {

if (r==0 || n==r) { return(1); } else {

return(combination(n-1,r) + combination(n-1,r-1)); } }

Note that the definition of combination(n,r) is recursive.

Yourassembly-language program must also use a recursive function call.

That is,your program should have a function that calls itself twice (to computecombination(n-1,r) and combination(n-1,r-1)) and adds the results together.This isn't the most efficient solution (e.g. n>9 will take a LONG time), butusing recursion will help you understand assembly-language procedure calls andstacks. Your program should input the value n and r from memory locationslabeled \in register 3 when theprogram halts. Straightforward solutions are possible in about 45 lines. Ifyour program is significantly longer than that, you are probably doingsomething wrong. Your program must be reasonably efficient, i.e. it mustexecute at most 5000 instructions for n=7, r=3 (this is several times slowerthan a good solution).

Passing parameters to subroutines can be confusing in assembly language.It's easiest if you make most registers callee-save so the caller can assumethe subroutine leaves those registers unchanged. These

callee-saved registersare typically pushed on the stack at the beginning of the subroutine and poppedoff the stack just before the subroutine returns.

Not all registers can or should be callee-save, however. For example, oneregister will contain the subroutine's return value. Also, the jalr instructionused to return from the subroutine will leave a register with a differentvalue than it held when the subroutine was called.

Here is a small LC-2K program that uses a subroutine call. It takesan input and calls a subroutine to compute the quantity 4*input.

Register 1 is used to pass input to the subroutine; register 3 is used bythe subroutine to pass the result back. The current top-of-stack (firstempty location) is given by stack + register 7.

lw 0 5 pos1 $5 = 1

lw 0 1 input $1 = memory[input] lw 0 2 subAdr prepare to call sub4n. $2=sub4n jalr 2 4 call sub4n; $4=return address; $3=answer halt

sub4n sw 7 4 stack save return address on stack add 7 5 7 increment stack pointer sw 7 1 stack save $1 on stack

add 7 5 7 increment stack pointer add 1 1 1 compute 2*input add 1 1 3 compute 4*input lw 0 2 neg1 $2 = -1

add 7 2 7 decrement stack pointer

本文来源:https://www.bwwdw.com/article/4bft.html

Top