CMPUT 229 - Computer Organization and Architecture I

Lab 2: Credit Card Validator

229 Lab 2: Credit Card Validator

Background

Validation

A valid credit card number must follows specific rules. Most websites run a validation algorithm to check that a credit card number is valid before accepting a form. There are numerous credit card validation algorithms that are in use today. The solution for this lab implements Luhn's algorithm, which was created by IBM scientist Hans Peter Luhn to validate a variety of identification numbers, including credit card numbers, IMEI numbers, and Social Insurance Numbers (SINs).

In addition to passing a validation test, a credit card number must also be regitered in a certified database in order to be a valid credit card number. Thus, if Luhn's algorithm is the underlying algorithm for a specific type of credit card validation, then an invalid Luhn's number cannot be a credit card number. After completing this lab, you'll have a basic understanding of how credit card validators function.

Classification

The first (left-most) digit of a credit-card number is known as the Major Industry Identifier (MII). The MII indicates whether the card is affiliated with an airline, another type of travel provider or other special groups. The MII may also encode constraints on how the funds might be used and what network the card belongs to. The table below shows some MIIs and their corresponding credit-card industries.


Industry Card Network MII
Airlines N/A 1
Airlines & Financial N/A 2
Banking & Financial Diner's Club 3
Banking & Financial VISA 4
Banking & Financial MasterCard 5
Merchandising & Banking Discover 6

Assignment

The solution for this lab must determine if a number is a valid credit-card number or not. If it is valid, then the solution must determine the kind of card. Write a RISC-V assembly function named creditValid with the following specifications:


            
                creditValid:
                This function decides if a given number is a valid credit-card number using Luhn's algorithm.

                Arguments:
                a0: pointer to an array of 32-bit integers, each integer represents a digit of the credit-card number
                a1: length of array
                a2: pointer to space reserved to store a modified array of digits

                Return:
                a0: type of card
                0 if card is invalid
                1 if card is VISA but not issued by Chase
                2 if card is MasterCard
                3 if card is Diner's Club
                4 if card is unknown
                12 if card is VISA issued by Chase
            
        

The first two parameters specify an array of integers. The address of the first element of the array is given in a0. Each element of the array is a 32-bit integer value that contains a number between 0 and 9 representing a digit. For example, if the card number is 1234, the array will be [1, 2, 3, 4]. a1 contains the length of the array. a2 is the address of a space reserved to store a modified credit card number after you have applied Luhn's algorithm.

Once creditValid determines the validity and the type of the credit-card number, it must return the appropriate return value in a0.

Writing a Solution

Notice the directive .include "common.s" in the solution template file provided. This directive will cause RARS to execute the code in the file common.s before executing any code in the solution. The code in common.s allocates space in memory, parses the number from test file into an array of digits, and then calls your creditValid function while passing pointer to allocated space in a2. You are encouraged to read the code in the common.s file to understand how the whole program works. A solution must begin with the label creditValid: or else it won't run.

In RISC-V assembly every function must have a return statement, this return statement is the instruction jr ra. You must include this instruction at the end of your function so that it will then return to the main program in common.s to display the result.

You don't need to follow register conventions in your solution for this lab, but its a good idea to be familiar with it for future labs. Return values must be stored in the argument registers. For this lab, you will only need to use the register a0 for the return value.

Part 1: Validation

Part 1 uses Luhn's algorithm to determine if a credit-card number is valid. The algorithm works as follows.

Step 1: Starting from the rightmost digit, double the value of every second digit.

Step 2: If doubling a number in Step 1 results in a two digit number, add the digits of the result to get a single digit number. For example, doubling the number 8 (8 x 2 = 16) results in a two digit number 16, so we will add the digits (1 + 6 = 7) to get 7.

Step 3: Add all the digits in the array. For example, if the array is [1, 2, 3, 4] then the sum is 1 + 2 + 3 + 4 = 10.

Step 4: If the sum is divisible by 10, then the number is valid. In other words, if the sum ends in 0 or the remainder (modulo) by 10 is 0, then the number is valid.




To facilitate the writing of the solution, it is useful to store the array created in Step 2. In order to not complicate the solution with memory allocation, space to store this array is pre-allocated and the address of this pre-allocated space is provided to creditValid by a2. This array is the same as the one above green text in the video (0:10).

Note: Luhn's algorithm works for numbers with any number of digits even though its use cases for credit-card check are typically limited to numbers between 10 and 20 digits.

Part 2: Classification

Part 2 determines the type of the credit card.

In this lab, cards are classified as: MasterCard, VISA, Chase, Diner's Club or unknown as specified in the table below.

Card Network MII Specifications
Diner's Club 3 First (left-most) three digits are between 300 and 305 inclusive. Length is 14
VISA 4 Length is 13 or 16
VISA Chase 4 First (left-most) five digits are 455953. Length is 13 or 16
MasterCard 5 Second digit is 1, 2 or 4. Length is 16

Note: This is not an exhaustive list. Some MasterCard cards used to begin with number 2 and some Diner's Club cards used to begin with 36, 38 and 309 as well. For the simplicity of this lab, we will only consider the above cases. MII is the first digit of credit card number. for example, MasterCard cards begin with the numbers 51, 52 or 54.

Hint: to check if the first 6 numbers are 455953, you can either check every single digit or you can compare the first 5 digits with an array. We have already created an array with a label vector: in the file creditValid.s. you can do a la register, vector to get the address of the first element of integer array vector .

If a card doesn't follow any of the above specifications but is valid, it is considered unknown.

Depending on the type of the card, you will store the result in a0 and return. If card is invalid, return 0. If the card is VISA, but not issued by Chase, return 1. If the card is VISA and issued by Chase, return 12. If the card is MasterCard, return 2. If the card is Diner's Club, return 3. If the card is unknown, return 4.

Testing your Lab

You are provided with some sample test cases to test your lab under Code/tests/. The correct answers for these tests provided in the files with same name but '.out' extension. To test your lab with test cases, provide the path to testfile at program arguments in RARS simulator. You can also test your lab from terminal with USAGE: rars LABFILE pa TESTFILE TESTFILE is the path to the file that contains the test case you want to test LABFILE is the path to the creditValid.s While writing test cases, please make sure that to have only one test case in each file. Make sure that your testfile contains only numbers. Don't create any test case which uses more than 16 digits. Some valid credit card numbers can be found at this website. Note: For the sake of this lab, please pay attention to Classification specification.

Assumptions and Notes

Resources

Marking Guide

Submission

There is 1 file to submit for this assignment:

For the file creditValid.s, make sure to keep the line ".include "common.s"" and make sure that you do not add a main label.