Assembly
Computer Organization and Assembly Languages, Fall 2012

Jump to...

assignment #1
assignment #3
assignment #4
final project
assignments


Assignment #2: TOY assembly

Assigned: 2012/10/18
Due: 2012/11/7 2:20pm

Description

In this project, you will have a chance to practice TOY assembly programming by writing a program to count number of 1-bit in an array and display the count in the BCD format. Note that you can only submit TOY assembly code but not TOY machine code. We provide an assembler and a linker for the TOY machine. You can download either Windows version or Linux version. In case that you need to compile them yourself, you can download the source codes toyasm.c and toylink.c. TOY simulator can be downloaded from here (a local copy). This file also contain a README file and several examples. The below is a summary for toyasm and toylink.
* Comments begin with semicolons. Please avoid Chinese characters and Big-5 encoding.

* Only support decimal literals or hexadecimal literals. Hexadecimal
  literals must begin with 0x

* Two assembly directives for data declararion
  DUP for declaring array
  DW for defining a number
  Examples:
  A	DUP	5	; define an array of five words
  B	DW	10	; declare a variable B, initialized with 10

* Labels must start with a letter. It can contain letters and numbers.
  No colons after labels.

* It is case-insensitive.

* Program will start from the first instruction that the assembler meets

* Instructions
  0 hlt	
  1 add	RD, RS, ST
  2 sub	RD, RS, ST
  3 and	RD, RS, ST
  4 xor	RD, RS, ST
  5 shl	RD, RS, ST
  6 shr	RD, RS, ST
  7 lda	RD, addr
  8 ld	RD, addr
  9 st	RD, addr
  A ldi	RD, RT
  B sti	RD, RT
  C bz	RD, addr
  D bp	RD, addr
  E jr	RT
  F jl	RD, addr

* To use assembler
  toyasm < reverse.asm > reverse.toy

* To generate an object file
  toyasm -o < mul.asm > mul.obj

* To link several object files together to make an executable file
  toylink multest.obj mul.obj stack.obj > multest.toy
						      
  Program will start from the first instruction of the first object file.

Wei-Ning Huang has provided a console-mode TOY virtual machine simulator which also supports GDB-like debug mode. It can be downloaded here.

Specification

The program is divided into three parts.
  1. (50%) Write a procedure BCD to convert a hexadecimal number in RA into its BCD (binary-coded decimal) representation and place it in RB. The return address should be placed in RF. For example, if RA=0x0024 before the call, RB=0x0036 after the all.
  2. (30%) Write a procedure CNT1 to count 1-bits in an array. The address of the array is placed at RA. The size of the array is in RC. The result should be placed in RB. The return address is RF. For example, if the array pointed by RA is {0x0F0F, 0x0111, 0x3300} and RC=3, after the call, RB=0x0F because there are 15 1's in the array.
  3. (20%) Write a main program to read a series of numbers from stdin until the input is zero. Count number of 1-bits in the input arrary (excluding the final zero you read) and display the count in the BCD format using stdout. (A test file can be download here. The answer is 0015 on TOY simulator.)
You can assume that the user inputs are less than 32 numbers and the number passed into BCD is less than 9999.

Submission

Submit your project online through the submission system submission system which will be available soon.