Docsity

BCD Add Program: A Assembly Language Implementation - Prof. Thomas F. Hain, Assignments of Computer Architecture and Organization

Toggle sidebar
bg1
#################################################################################
# PROGRAM: BCD_ADD
# Author: T. Hain
# Assignment: 2
# Last modified: March, 2009
#
#################################################################################
# Program functional description:
#
# Program to read 2 long (<64 digits) signed decimal strings from standard input,
# convert them to bcd values, add bcd values, and write decimal string to standard output.
#
#################################################################################
.data
test: .word 0x32111113, 0x00987654
.ascii "+"
string: space 64
bcd1: .space 32
bcd2: .space 32
bcdsum: .word 8
.text
main: la $a0, string # $a0 = address of input buffer
li $a1, 64 # $a1 = size of input buffer
li $v0, 8 # system call code for reading first decimal string
syscall
la $a1, bcd1
jal tobcd # convert to bcd
la $a0, string # $a0 = address of input buffer
li $a1, 64 # $a1 = size of input buffer
li $v0, 8 # system call code for reading second decimal string
syscall
la $a1, bcd2
jal tobcd #convert to bcd
la $a0, bcd1
la $a1, bcd2
la $a3, bcdsum
# jal addbcd # add bcd
la $a0, string
la $a1, bcdsum
# jal todec # convert to decimal string
la $a0, string
li $v0, 4 # system call code for print string
syscall
li $v0, 10 # system call code for exit
syscall
pf3

Partial preview of the text

Download BCD Add Program: A Assembly Language Implementation - Prof. Thomas F. Hain and more Assignments Computer Architecture and Organization in PDF only on Docsity!

# PROGRAM: BCD_ADD

Author: T. Hain

Assignment: 2

Last modified: March, 2009

#################################################################################

Program functional description:

Program to read 2 long (<64 digits) signed decimal strings from standard input,

convert them to bcd values, add bcd values, and write decimal string to standard output.

################################################################################# .data test: .word 0x32111113, 0x .ascii "+" string: space 64 bcd1: .space 32 bcd2: .space 32 bcdsum: .word 8 .text main: la $a0, string # $a0 = address of input buffer li $a1, 64 # $a1 = size of input buffer li $v0, 8 # system call code for reading first decimal string syscall la $a1, bcd jal tobcd # convert to bcd la $a0, string # $a0 = address of input buffer li $a1, 64 # $a1 = size of input buffer li $v0, 8 # system call code for reading second decimal string syscall la $a1, bcd jal tobcd #convert to bcd la $a0, bcd la $a1, bcd la $a3, bcdsum

jal addbcd # add bcd

la $a0, string la $a1, bcdsum

jal todec # convert to decimal string

la $a0, string li $v0, 4 # system call code for print string syscall li $v0, 10 # system call code for exit syscall

Funtion tobcd

Description:

Procedure to convert buffer containing hexadecimal string to binary value.

Input: $a0 = address of '\n' terminated decimal string (with possible leading sign)

$a1 = address of 8-word bcd buffer (for converted bcd value)

#################################################################################

Register usage:

$a0 = address of '\n' terminated decimal string (with possible leading sign)

$a1 = address of bcd buffer converted value

$t0 = moving pointer to character in decimal string

$t1 = moving pointer to byte (2 nibbles) in bcd buffer

$t2 = odd character at address in $t

$t3 = numeric charcter count in decimal string

$t4 = even character at address in $t

################################################################################# tobcd: li $t7, '\n' li $t9, '-' move $t0, $a move $t1, $a l2: lb $t2, 0($t0) beq $t2, $t7, eos addiu $t0, $t0, 1 j l eos: sub $t3, $t0, $a0 # $t3 = length of string (incl sign) addi $t0, $t0, -1 # move char point back to rightmost char lb $t2, 0($t0) # load least signficant digit addi $t2, $t2, -48 # convert to nibble sll $t2, $t2, 4 # shift left 4 bit to make room for numwords/sign sw $zero, 0($t1) # zero out bcd buffer sw $zero, 4($t1) sw $zero, 8($t1) sw $zero, 12($t1) sw $zero, 16($t1) sw $zero, 20($t1) sw $zero, 24($t1) l3: sb $t2, 0($t1) # store buffer byte addi $t1, $t1, 1 # move to next byte in bcd buffer addi $t0, $t0, -1 # move char pointer to left lb $t2, 0($t0) # load even digit (from right) addi $t2, $t2, -48 # convert to nibble bltz $t2, done # make sure char is not sign addi $t0, $t0, -1 # move char pointer to left lb $t4, 0($t0) # load odd digit (from right) addi $t4, $t4, -48 # converto to nibble bltz $t4, done # make sure char is not sign sll $t4, $t4, 4 # shift odd nibble 4 bits to left add $t2, $t2, $t4 # concatenate two nibble j l done: bne $t0, $a0, nosign # check if string started with a sign char addi $t3, $t3, -1 # if so, adjust digit count nosign: srl $t3, $t3, 3 # divide digit count by 8 to get # additional words sll $t3, $t3, 1 # move this number to left 1 bit to make room for sign bit lb $t2, 0($t0) bne $t2, $t9, notneg addi $t3, $t3, 1 # set the sign bit notneg: lb $t2, 0($a1) # reload first byte of buffer (least significant nibble << 4) add $t2, $t2, $t3 # add numwords/sign sb $t2, 0($a1) # store in buffer jr $ra