im making a basic ASM simulator which runs in a datagridview, iv got most of it working like the add/sub/mul/div operators, but im having trouble with the jump function, the jump it self works, the simulator jumps but it still executes the next operator after the jump when its supposed to execute from operator/line its jumped too.
e.g
#1 - jmp : 3
#2 - mul : 5
#3 - add : 7
-- It should skip line two and start the simulation from line 3:

For Each row As DataGridViewRow In DataGridView2.Rows
If row.Cells.Item("ColOperator").Value = "JMP" Then
                        If row.Cells.Item("ColOperand").Value <> "" Then
                            Dim s As String = row.Cells.Item("ColOperand").Value
                            DataGridView2.ClearSelection()
                            For x As Integer = 0 To DataGridView2.Rows.Count - 1
                                If CStr(DataGridView2.Rows(x).Cells(0).Value).StartsWith(s) Then
                                    DataGridView2.FirstDisplayedScrollingRowIndex = x
                                    DataGridView2.Item(0, x).Selected = True
                                    DataGridView2.Rows(x).Selected = True
                                    TxtPc.Text = x
                            End If
                            Next
                      End If
                    End If

' took out the other functions to make it cleaner, i can post all the code if needed.

next

its got something to do with the for each loop, hmmm

Recommended Answers

All 3 Replies

Does the leftmost cell actually start with "#"? If it does then it looks like your match will always result in True for any line because you are testing using StartsWith. Are you sure you don't want to test the target of the jmp ("3" in your example) with cell(0) starting just after the #? Wouldn't it be easier to strip the # from column 0 and just compare "3" to the remaining contents as in

1   jmp  3
2   mul  5
3   add  7

Even if you removed the #, using StartsWith would cause "3" to match "3", "30", "300", etc.

Fixed: changed the for each to:
Do While rw < line And finished = False

My mind just threw a StackOverflowException from thinking about this.

A high-level language, simulating a low level language, which is powered by a high level language, that gets converted to a low level language.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.