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A-F0-9]{1,8})
|
(?<bp0>[-+]?[0oO]?[bB][01]{1,32})
|
(?<op0>[-+]?[0oO][0-7]+)
|
(?<dp0>[-+]?\\#?\\d+)
|
(?<lp0>[a-zA-Z0-9_]+)
)
[ \\t]*
)?
(?:[ \\t]*(?<comment>[!;].*)?)
$', 'gm')
const str = `!============================================================
! CS-2200 Homework 1
!
! Please do not change main's functionality,
! except to change the argument for factorial or to meet your
! calling convention
!============================================================
main: la \$sp, stack ! load address of stack label into \$sp
lw \$sp, 0(\$sp) ! FIXME: load desired value of the stack
! (defined at the label below) into \$sp
la \$at, factorial ! load address of factorial label into \$at
addi \$a0, \$zero, 5 ! \$a0 = 5, the number to factorialize
jalr \$at, \$ra ! jump to factorial, set \$ra to return addr
halt ! when we return, just halt
factorial:
sw \$a0, 0(\$sp) ! put i, the first parameter, on stack
addi \$sp, \$sp, -4 ! leave space for argument, return value, return address, and old frame pointer
sw \$fp, 1(\$sp) ! store old frame pointer
addi \$fp, \$sp, 0 ! frame pointer points to first local variable
sw \$ra, 2(\$fp) ! store return address
lw \$t0, 4(\$fp) ! load i to \$t0
beq \$t0, \$zero, base ! if i == 0, branch to base case
addi \$a0, \$t0, -1 ! \$a0 = i - 1
addi \$sp, \$sp, -1 ! start a new frame
jalr \$at, \$ra ! calls factorial(i - 1)
lw \$t2, 0(\$sp) ! \$t2 = factorial(i - 1)
addi \$sp, \$sp, 2 ! pop return value and argument from stack
lw \$t0, 4(\$fp) ! load argument from run-time stack
addi \$t1, \$zero, 0 ! \$t1 is the counter
addi \$v0, \$zero, 0 ! initialize \$v0 as 0
mult: beq \$t1, \$t2, exit ! branch to exit if equal
add \$v0, \$v0, \$t0
addi \$t1, \$t1, 1
beq \$zero, \$zero, mult
base: addi \$v0, \$zero, 1 ! \$v0 stores 1
sw \$v0, 0(\$fp) ! store result in memory for fn
return: lw \$v0, 0(\$fp) ! load return value from fn
sw \$v0, 3(\$fp) ! save return value
lw \$ra, 2(\$fp) ! restore return address
addi \$sp, \$fp, 3 ! stack pointer points to return value
lw \$fp, 1(\$fp) ! restore frame pointer
jalr \$ra, \$zero ! return
exit: sw \$v0, 0(\$fp) ! store result in memory for fn
beq \$zero, \$zero, return
stack: .word 0x4000 ! the stack begins here (for example, that is)
`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions