Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support .align directive as power of 2 #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/main/kotlin/venus/assembler/Assembler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,22 @@ internal class AssemblerPassOne(private val text: String) {
args.forEach(prog::makeLabelGlobal)
}

".float", ".double", ".align" -> {
".align" -> {
checkArgsLength(args, 1)
val pow2 = userStringToInt(args[0])
if (pow2 < 0 || pow2 > 8) {
throw AssemblerError(".align argument must be between 0 and 8, inclusive")
}
val mask = (1 shl pow2) - 1 // Sets pow2 rightmost bits to 1

/* Add padding until data offset aligns with given power of 2 */
while ((currentDataOffset and mask) != 0) {
prog.addToData(0)
currentDataOffset++
}
}

".float", ".double" -> {
println("Warning: $directive not currently supported!")
}

Expand Down
25 changes: 25 additions & 0 deletions src/test/kotlin/assembler/AssemblerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,29 @@ class AssemblerTest {
sim.step()
assertEquals(0b10001, sim.getReg(9))
}

@Test fun alignTest() {
val (prog, _) = Assembler.assemble("""
.data
.align 3
one: # 8-byte aligned
.byte 1
.align 3
two: # 8-byte aligned
.byte 2
.align 2
three: # 4-byte aligned
.byte 3
.text
la a1, one
la a2, two
la a3, three
sub x5, a2, a1 # Should be 8
sub x6, a3, a2 # Should be 4
""")
val sim = Simulator(Linker.link(listOf(prog)))
sim.run()
assertEquals(8, sim.getReg(5))
assertEquals(4, sim.getReg(6))
}
}