Grace Hopper Center

CSC 215 Weekly Assignments: Week 17

CSC 215


Overview

Happy New Year! This week we will continue to develop the arbitrary precision integer in C on the Intel 8080 processor that we began the week before winter break.

Friday and Monday, January 9th and 12th

Classwork / Homework / Evaluation

Note: If you're in the A day section, you'll be doing this before the add_bigints() function is due.

Today we will handle negative values, both with a sub_bigints() function and by handling a unary minus (-) on a bigint.

You know the dance by now:

  1. Add a test for your function expressing how you intend it to work (which should fail).
  2. Write code to make the test pass.
  3. Interate until you have a robust, well tested function.

After Toby pointed out I was leaking memory when running my tests, I changed the code from last class to fix that. Toby also made a convincing argument for changing the order of the parameters in set_bigint() so that the variable being set is the first parameter. This is consistent with how Kernighan and Ritchie do things (see the strcpy() function defined on page 100 of The C Programming Language for example), so it is a good idea indeed to do what they do.

Gabe C. continues to greatly enrich our learning experience with his initiative. He did some informed AI querying, and came up with information about our BDS C compiler that will be most helpful to us. I put what he sent me in my git repo notes here.

For homework, read from pages 35 through 44 in The Large Integer Case Study in C++. Take notes on this reading and make sure you understand the implementation questions it discusses for subtraction, multiplication, and exponentiation. Be prepared to answer conceptual questions about these implementations in a quiz at the beginning of our next class. While I don't think you'll need it if you really understand the reading, Abi requested, and I agreed to allow you one 8 1/2 by 11 sheet of paper front and back of notes to use while taking the quiz.

Wednesday and Thursday, January 7th and 8th

Intro

I got a delightful email from Gabe C. in which he shared Z80-μLM: A Retrocomputing Micro Language Model. I haven't had a chance to take a look at this yet, but we certainly should.

Liam Brown, Arlington Tech / NVCC graduate in the class of 2024, will be with us this Spring working on his sophmore project, exploring how much of our Data Structures and Anaylsis of Algorithms curriculum can effectively be taught using CP/M and BDSC.

Thanks to Jake, we now know what the problem is with the failing test. I had the parenthesis in the wrong place! That discovery lead us down a rabbit hole about how the BDSC compiler works, which we'll discuss more later.

Classwork / Homework / Evaluation

Here are your tasks due Friday:

  1. Fix the parenthesis and make the set_bigint(), get_bigint() test pass.
  2. Add tests for a function add_bigints() which take two bigints as arguments and return their sum. Try to be as exhautive as you can. Do you have tests for single digit bigints? Double digit bigints? Ten digit bigints? bigints with different numbers of digits, both with the first and second arguments being the shorter of the two?
  3. Write the function to make the tests pass.

This is due by Friday at 11:59 pm so I have the weekend to evaluate your work. As usual, your commmit history should show work both during and between classes.

Monday and Tuesday, January 5th and 6th

Intro

We'll start the new year by introducing a testing tool that two students, Akshay Kuchibhatla and Caleb O'Neil, developed last year. You can find their code and documentation here.

Here is a struct we can use to start our bigint data type, split it two files: BIGINT.H:

struct bigint {
    char negative;
    char numdigits;
    char* digits;
};

void set_bigint();
char* get_bigint();

and BIGINT.C:

#include <stdio.h>
#include "BIGINT.H"

void set_bigint(num, numstr)
char *numstr;
struct bigint *num;
{
    char last_pos, i;
    num->negative = (numstr[0] == '-');
    num->numdigits = strlen(numstr) - num->negative;
    num->digits = alloc(num->numdigits);
    last_pos = num->numdigits + (num->negative ? 0 : -1);

    for (i = 0; i < num->numdigits; i++) {
        num->digits[i] = numstr[last_pos-i];
        /* printf("numstr[%d] is %c\n", last_pos-i, numstr[last_pos-i]); */
    }
}

char* get_bigint(num)
struct bigint *num;
{
   char *numstr;
   char start_pos, i;
   numstr = alloc(num->numdigits + (num->negative ? 2 : 1));
   start_pos = num->negative;
   if (start_pos == 1) numstr[0] = '-';
   for (i = 0; i < num->numdigits; i++) {
       numstr[i+start_pos] = num->digits[num->numdigits-(i+1)];
       /* printf("numstr[%d] is %c\n", i, numstr[i+start_pos]); */
   }
   numstr[num->numdigits+start_pos] = '\0';
   return numstr;
}

Here is a program, BIGINTTD.C, to test it:

#include "BIGINT.H"
#include "BDSCTEST.H"

main() {
    START_TESTING("BIGINTTD.C");
    char *bis;

    TEST_CASE("Read and write bigint 1234567") {
        struct bigint bi;
        set_bigint(&bi, "1234567");
        bis = get_bigint(&bi)
        ASSERT_STR(bis, "1234567");
        free(bis);
    }

    END_TESTING();
}

Over the winter break I compiled and linked these with the following sequence of commands:

I>cc bigint
I>cc biginttd
I>clink biginttd bigint

which generated BIGINTTD.COM. Running it, I got:

BigInt failing test

I did not figure out why these tests are failing. That will be our first task for 2026.

Classwork / Homework / Evaluation

Everyone should reproduce the failing tests from the screenshot on their own computers.

Our goal will be to get these first tests working to be able to read and write our BigInt values. That's a big lift, I understand, so we will adjust plans and expectations according to our progress.