All files / simulator/src/simulator assembler.ts

96.09% Statements 123/128
96.15% Branches 50/52
80% Functions 8/10
96.06% Lines 122/127

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305                          5x                                                     19x 19x 19x   19x 19x   19x 19x   19x 1390x 526x   526x 19x 19x 19x 507x 19x 19x 19x 488x 53x   6x     47x 47x 47x 47x     53x 435x 435x   76x 76x 76x 76x     359x 359x   359x 28x 28x 28x 15x 15x       359x     412x     19x                           17x                 17x 17x   17x   17x 317x 317x 317x   317x 24x 24x 24x 24x   24x 13x 13x 13x     13x 13x   293x   2x 2x 2x 2x 2x   291x 291x 118x 28x 28x 28x 28x 90x 2x 2x 2x 2x   88x 88x 88x 88x   118x 173x 158x 6x 6x 6x       152x 27x       27x 27x 125x           52x 52x 52x 52x   73x 73x   73x         158x 15x 15x 15x     317x   317x 317x     17x                             16x 16x 44x   44x   16x         15x   15x 15x   15x                                 8x             8x                                                    
import {
  BYTES_PER_WORD,
  instT,
  instList,
  MEM_DATA_START,
  MEM_TEXT_START,
  section,
  symbolT,
  SYMBOL_TABLE,
  resetSymbolTable,
} from '@utils/constants';
import {numToBits, symbolTableAddEntry, toHexAndPad} from '@utils/functions';
 
export const makeSymbolTable = (inputs: string[]) => {
  /*
   * make symbol table from assembly file
   * using SYMBOL_TABLE in constants.ts
   * 
   * 'dataSeg'에 data 저장
   * 'textSeg'에 text 저장
   * 
   * .text 
   * - indicates that following items are stored in the user text segment, typically instructions 
   * - It always starts from 0x400000 (MEM_TEXT_START)
 
   * .data 
   * - indicates that following data items are stored in the data segment 
   * - It always starts from 0x10000000 (MEM_DATA_START)
   * 
   * return
   * {
   *    dataSeg : dataSeg, 
   *    textSeg : textSeg, 
   *    dataSectionSize : dataSectionSize, 
   *    textSectionSize : textSectionSize
   * }
   * 
   * [USAGE EXAMPLE] 
   * const {dataSeg, textSeg, dataSectionSize, textSectionSize} = makeSymbolTable(inputs);
  */
  resetSymbolTable();
  let address = 0;
  let curSection = section.MAX_SIZE;
 
  let dataSectionSize = 0;
  let textSectionSize = 0;
 
  const dataSeg: string[] = [];
  const textSeg: string[] = [];
 
  inputs.forEach((input: string) => {
    const splited: string[] = input.split('\t').filter(s => s !== ''); // ex. ['array:', '.word', '3']
    const symbol: symbolT = new symbolT();
 
    if (splited[0] == '.data') {
      curSection = section.DATA;
      address = MEM_DATA_START;
      return;
    } else if (splited[0] == '.text') {
      curSection = section.TEXT;
      address = MEM_TEXT_START;
      return;
    } else if (curSection === section.DATA) {
      if (splited.length === 2) {
        // ex. ['.word','123']
        dataSeg.push(splited[1]);
      } else {
        // ex. ['array:', '.word', '3']
        symbol.address = address;
        symbol.name = splited[0].replace(':', '');
        symbolTableAddEntry(symbol);
        dataSeg.push(splited[2]);
      }
 
      dataSectionSize += BYTES_PER_WORD;
    } else Eif (curSection === section.TEXT) {
      if (splited.length === 1) {
        // ex. ['main:']
        symbol.name = splited[0].replace(':', '');
        symbol.address = address;
        symbolTableAddEntry(symbol);
        return;
      } else {
        // ex. ['and', '$17, $17, $0']
        const name: string = splited[0];
        textSeg.push(input); // ex. 'and	$17, $17, $0'
 
        if (name === 'la') {
          const targetSymbol: string = splited[1].split(' ')[1]; // ex. 'data1'
          const targetAddress: string = toHexAndPad(SYMBOL_TABLE[targetSymbol]);
          if (targetAddress.slice(4) !== '0000') {
            textSectionSize += BYTES_PER_WORD;
            address += BYTES_PER_WORD;
          }
        }
      }
      textSectionSize += BYTES_PER_WORD;
    }
 
    address += BYTES_PER_WORD;
  });
 
  return {dataSeg, textSeg, dataSectionSize, textSectionSize};
};
 
export function recordTextSection(textSeg: string[]): [string[], number[][]] {
  /*
   * parameter로 textSeg를 받는다.
   * textSeg 있는 text들 한 줄 씩 체크해서 binaryText 리스트에 바이너리 문장으로 추가
   * 명령어 타입별(R, I, J)로 명령어 이름별로 묶어서 번역
   *
   *  binaryText 이라는 list에 명령어를 번역한 binary 문장을 한 줄씩 추가
   *  return binaryText
   *  binaryText: ['00000000000000000000000001011000', '00000000000000000000000000001100']
   */
 
  let curAddress: number = MEM_TEXT_START;
  let instruct: string[];
  let address: number;
  let rs: string;
  let rt: string;
  let rd: string;
  let imm: string;
  let shamt: string;
  let immReg: string;
  const binaryText: string[] = [];
  const mappingTable: number[][] = [];
 
  let binaryInstructionCounter = 0;
 
  textSeg.forEach((text, i) => {
    mappingTable.push([]);
    instruct = text.slice(1).replace(/ /g, '').split(/,|\t/);
    const opName: string = instruct[0];
 
    if (opName === 'la') {
      address = SYMBOL_TABLE[instruct[2]];
      rt = numToBits(Number(instruct[1].replace('$', '')), 5);
      imm = numToBits(parseInt(address.toString(16).slice(0, 4), 16), 16);
      binaryText.push('001111' + '00000' + rt + imm);
 
      if (address.toString(16).slice(4, 8) !== '0000') {
        imm = numToBits(parseInt(address.toString(16).slice(4, 8), 16), 16);
        binaryText.push('001101' + rt + rt + imm);
        curAddress += BYTES_PER_WORD;
 
        // if two binary instructions are made by la instruction, then it should map 'current assembly instruction' to 'two binary instructions'
        mappingTable[i].push(binaryInstructionCounter);
        binaryInstructionCounter++;
      }
    } else if (opName === 'move') {
      //op = ADD op "000000"
      rs = numToBits(Number(instruct[2].replace('$', '')), 5);
      rt = '00000';
      rd = numToBits(Number(instruct[1].replace('$', '')), 5);
      shamt = '00000';
      binaryText.push('000000' + rs + rt + rd + shamt + '100000'); //funct = "100000"
    } else {
      const opInfo: instT = instList[opName];
      if (opInfo.type === 'R') {
        if (opInfo.name === 'sll' || opInfo.name === 'srl') {
          rs = '00000';
          rt = numToBits(Number(instruct[2].replace('$', '')), 5);
          rd = numToBits(Number(instruct[1].replace('$', '')), 5);
          shamt = numToBits(Number(instruct[3]), 5);
        } else if (opInfo.name === 'jr') {
          rs = numToBits(Number(instruct[1].replace('$', '')), 5);
          rt = '00000';
          rd = '00000';
          shamt = '00000';
        } else {
          rs = numToBits(Number(instruct[2].replace('$', '')), 5);
          rt = numToBits(Number(instruct[3].replace('$', '')), 5);
          rd = numToBits(Number(instruct[1].replace('$', '')), 5);
          shamt = '00000';
        }
        binaryText.push(opInfo.op + rs + rt + rd + shamt + opInfo.funct);
      } else if (opInfo.type === 'I') {
        if (opInfo.name === 'lui') {
          rt = numToBits(Number(instruct[1].replace('$', '')), 5);
          rs = '00000';
          imm =
            instruct[2].slice(0, 2) === '0x'
              ? numToBits(parseInt(instruct[2].slice(2), 16), 16)
              : numToBits(Number(instruct[2]), 16);
        } else if (opInfo.name === 'beq' || opInfo.name === 'bne') {
          imm = numToBits(
            Number((SYMBOL_TABLE[instruct[3]] - (curAddress + 4)) / 4),
            16,
          );
          rs = numToBits(Number(instruct[1].replace('$', '')), 5);
          rt = numToBits(Number(instruct[2].replace('$', '')), 5);
        } else if (
          opInfo.name === 'lw' ||
          opInfo.name === 'lhu' ||
          opInfo.name === 'sw' ||
          opInfo.name === 'sh'
        ) {
          immReg = instruct[2].split('(')[1].split(')')[0];
          rs = numToBits(Number(immReg.replace('$', '')), 5);
          rt = numToBits(Number(instruct[1].replace('$', '')), 5);
          imm = numToBits(Number(instruct[2].split('(')[0]), 16);
        } else {
          rs = numToBits(Number(instruct[2].replace('$', '')), 5);
          rt = numToBits(Number(instruct[1].replace('$', '')), 5);
 
          imm =
            instruct[3].slice(0, 2) === '0x'
              ? numToBits(parseInt(instruct[3].slice(2), 16), 16)
              : numToBits(Number(instruct[3]), 16);
        }
        binaryText.push(opInfo.op + rs + rt + imm);
      } else Eif (opInfo.type === 'J') {
        address = Number(SYMBOL_TABLE[instruct[1]]) / 4;
        binaryText.push(opInfo.op + numToBits(address, 26));
      }
    }
    curAddress += BYTES_PER_WORD;
 
    mappingTable[i].push(binaryInstructionCounter);
    binaryInstructionCounter++;
  });
 
  return [binaryText, mappingTable];
}
 
export function recordDataSection(dataSeg: string[]): string[] {
  /*
   * input값을 dataSeg를 받는다.
   * dataSeg에 있는 data들 한 줄 씩 체크해서 binaryData 리스트에 바이너리 문장으로 추가
   * data값을 그대로 binary 문자로 번역
   *
   *  binaryData 이라는 list에 명령어를 번역한 binary 문장을 한 줄씩 추가
   *  return binaryData
   *  ex) binaryData: ['00000010001000001000100000100100', '00000010010000001001000000100100']
   */
 
  let dataNum: number;
  const binaryData: string[] = [];
  for (const data of dataSeg) {
    dataNum =
      data.slice(0, 2) === '0x' ? parseInt(data.slice(2), 16) : Number(data);
    binaryData.push(numToBits(dataNum));
  }
  return binaryData;
}
 
export function makeBinaryObject(inputs: string[]) {
  const {dataSeg, textSeg, dataSectionSize, textSectionSize} =
    makeSymbolTable(inputs);
  const [binaryText, mappingTable]: [string[], number[][]] =
    recordTextSection(textSeg);
  const binaryData: string[] = recordDataSection(dataSeg);
 
  return {
    dataSectionSize,
    textSectionSize,
    binaryText,
    binaryData,
    mappingTable,
    dataSeg,
    textSeg,
  };
}
 
export function makeBinaryArray(
  dataSectionSize: number,
  textSectionSize: number,
  binaryText: string[],
  binaryData: string[],
): string[] {
  const output: string[] = [
    numToBits(textSectionSize, 32),
    numToBits(dataSectionSize, 32),
    ...binaryText,
    ...binaryData,
  ];
 
  return output;
}
 
export function makeBinaryString(
  dataSectionSize: number,
  textSectionSize: number,
  binaryText: string[],
  binaryData: string[],
): string {
  /*
   * output에 text 문장 개수를 binary로 번역해서 추가
   * output에 data 개수를 binary로 번역해서 추가
   *
   */
  const binarySize: string[] = [
    numToBits(textSectionSize, 32),
    numToBits(dataSectionSize, 32),
  ];
  let output = '';
 
  binarySize.concat(binaryText, binaryData).map(binaryLine => {
    output += `${binaryLine}\n`;
  });
 
  return output;
}