4

can I do something like this on Assembly bne jsr swap, if not how can I solve this problem from C, thanks in advance

if(start!=pivot_index){
  swap(board,start,pivot_index);
 }

I was taught that I must write jsr and sub-routine but can I do something like this bne sub-routine

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
lego69
  • 1,403
  • 3
  • 12
  • 15

3 Answers3

4

In assembly that would usually be translated into something like this (pseudo-assembly):

load [start]
compare [pivot_index]
branch-if-equal label1
push [pivot_index]
push [start]
push [board]
call swap
add-stack-pointer 12
label1:

ie. the if statement is converted into a jump that jumps over the body of the if if the controlling expression is not true.

caf
  • 233,326
  • 40
  • 323
  • 462
2

Sure you can do this. On x86 you need two branches:

  # assume EAX = start, EBX = pivot_index

  cmp eax, ebx
  beq .SkipSwap

  call swap

.SkipSwap:

For ARM assembly it's easier because you can use a conditional branch:

  # assume r0 = start, r1 = pivot_index

  cmp  r0, r1
  blne swap
Nils Pipenbrinck
  • 83,631
  • 31
  • 151
  • 221
0

No, you cannot do bne subroutine instead of jsr subroutine, because jsr means "jump setting return".

The difference between that and the conditional branch instructions is that the jsr pushes the return address onto the stack, so the subroutine knows where to return. If you just branch to the subroutine using bne, there's no return address saved, so the subroutine doesn't know where to return when done.

caf's answer shows you the typical way you would handle that, you just need to translate it into PDP-11 operations.

David Gelhar
  • 27,873
  • 3
  • 67
  • 84