1

so we are using a pdp 11 simulator and i am trying to subtract one register from another this way:

sub r2, r4

what i want is that r4 will contain the difference between the values kept in r2 and r4, instead i get a huge, unrelated number. i tried sub r2, r5 and its the same problem.

where am i wrong? here's the code:(the line is after the flag "kaka")

.=torg+1000
main:

mov n_cols, r0
mul n_rows, r0
mov r1, r0;     r0 is now the length of the array of the maze
mov #Board, r2
mov #Path,r3;
loop:
cmpb (r2),#'S
beq loop2
tst (r2)+
sob r0, loop ;Go to next iteration

loop2:
cmpb (r2), #1
beq illegal
mov #Board,r4
kaka:
sub r2, r4
waka:
bmi illegal

edit: this is not the complete code, the rest of the code is not related and the problem occurs even when the rest of the code is marked as comment.

timsa7
  • 69
  • 3
  • 9

1 Answers1

4

Its been decades since I played with PDP 11 assembler; but sub r2, r4 subtracts r4 FROM r2. You should use sub r4, r2 and change the rest of the code accordingly.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • yeah its an old computer, but i am trying to subtract r4 from r2 and put it in r4. now i noticed that the huge number i get in r4 is the 2 complement of the real value i want! what is wrong in here? – timsa7 May 03 '11 at 21:21
  • 1
    `sub r4,r2` is like writing `r4 = r4 - r2`. That will give you the two's complement of `r2 - r4`. Just `neg r4` and you have your answer. – Jim Mischel May 03 '11 at 21:34
  • `mov r1, r2` `sub r1, r4` `mov r4, r1` – Richard Schneider May 03 '11 at 21:37
  • 1
    You might notice that r2 - r4 is also -r4 + r2, so why not try `neg r4` `add r4, r2` ? – Bo Persson May 04 '11 at 16:31
  • I'm not familiar with PDP-11 and even haven't got a chance to work with the PDP machines in the old times, but I have just looked up in PDP-11 Handbook from 1969: sub src,dst makes dst < dst-src, while cmp src,dst makes flags < src-dst. The reason for the topicstarter to get strange results is probably that r2 points to some memory past Board label after some loops, while r4 loaded with Board. So when subtracting r2 from r4, he gets negative number which could be also interpreted as huge positive integer. If he needs to get positive difference, he could do "sub r4,r2" and get difference in r2. – lvd Oct 06 '15 at 06:35