I have to sort an array given by ARY1, Sort it down in the SORT function, then store the sorted array into ARY1S. I chose not to include ARY2, but it would work identical. The SORT should work for both ARY1 AND ARY2. I took the bubblesort algorithm(C++) and tried my best to convert it into assembly, but I have run into errors.
My D0 is length of array
My D1 would be i
My D2 would be j
and D3 is temp
Here is my attempt(this does not work) not only because of syntax issues:
ARY1 DC.B 7 //array size
DC.B 56,-5,8,23,-9,18,4 //my array
ARY1S DS.B 7 //storage size
SORT1 MOVEA.L #ARY1, A0 //moving values into A0
MOVEA.L #ARY1S, A1
MOVE.B (A0)+, D0 //Storing size (7) into D0
BSR SORT
SORT CMP.B D0,D1
BGE DONE
LOOP CMP.B D0,D2
BGE INC
CMP (A1,D2),(A1,D1) //this is not compiling/wrong syntax
BLE NOTLESS
MOVE.B (A1,D2),D3
MOVE.B (A1,D1),(A1,D2)
MOVE.B D3,(A1,D1)
NOTLESS ADDQ #1, D2
BRA LOOP
INC ADDQ #1, D1
MOVE.B D1,D2
BRA SORT
DONE RTS
Also, if someone can help with the line of code that I commented is not working. Prompting syntax error.
CMP (A1,D2),(A1,D1) //this is not compiling/wrong syntax
I suggest that you download and use the M68000 Programmer's Reference Manual.
In it, you'll find that the only valid form of CMP is CMP <ea>,Dn. In other words, you'll have to first move the value at (A1, D1) into a D-register, and then compare it against (A1, D2).
Try this instead:
CMP.B A1,D2
CMP.B A1,D1
Related
How do I modify this LC-3 code to make it count the number of 0s instead of ones
.ORIG x3000
LD R2, INPUT
AND R0, R0, #0 ; COUNTER INITIALIZED TO ZERO
ADD R1, R0, #1
ADD R3, R0, #15
LOOP
AND R4, R2, R1
BRz SKIP
ADD R0, R0, #1
SKIP
ADD R1, R1, R1
ADD R3, R3, #-1
BRzp LOOP
INPUT .FILL x1997
.END
Have you watched how it works in the debugger? Single step this small program, and you'll see how it works.
Can you find the if-then construct in here? An if-then construct has the form if-condition-then-action. So, you're looking for something that has this effect:
if ( bit is set )
R0++;
And you want to change it to the opposite condition, so it counts zeros instead.
if ( bit is clear )
R0++;
Where bit is clear is the same as ! (bit is set), i.e. it is the opposite.
Note that the above is in terms of structured language like C. In assembly, if-condition-then-action looks more like this:
if ( condition is false ) goto Label1; // action is skipped if condition is false
..action.. // action runs if condition is true
Label1:
This is because the only control structure in assembly/machine code is conditional branch, which in C looks like if ( condition ) goto label;
If, i suppose, your program is OK (i don't try it) a quick modification is to flip all 0 to 1 and 1 to 0.
NOT do the job!
I leave you the exercise.
Another way, is that if you count n bit1 this mean that you have 16-n bit0...
Again NOT is your friend to compute 16-n
Note that both leave the code intact which can be used as a subroutine.
Don't forget to use HALT to stop the program
I ve this code, there we have a distance (D) and an angle (A), the functions must returns X, x=cosine(a)*d and Y, y=sine(a)*d
.data
n180 word 180
d word 60
a word 10
x word 0
y word 0
.code
fild word ptr a
fild word ptr n180
fdiv
fldpi
fmul
fsincos
fild word ptr d
fmul
fistp word ptr x
fwait
fxch
fild word ptr d
fmul
fistp word ptr y
at first time i run the program (using a=10 and d=60) i get X=59 e Y=-32768
here X is right, i get it before use FXCH but Y is wrong. if i run the program again then i get X=59 and Y=10
now it's ok
Why first time I run I get an error with FXCH?
For completeness...
... the no-operand FADD, FDIV, FDIVR, FMUL, FSUB and FSUBR all pop the right-hand argument ST(0) (so the left-hand argument ST(1) is replaced by the result, and then the stack is popped and the result becomes ST(0)).
... so the FXCH is not required.
... indeed, without the FXCH your sequence of operations leaves the FPU register stack in the same state as it started in (assuming it does not overflow at any point), which is generally a Good Thing.
I note that both FADD and FADDP etc. (with no operands) are just shorthand for FADDP ST(1), ST(0) (Intel ordering) and that is not the same as FADD ST(1), ST(0). I note also that the Intel manual prefers the FADDP etc. mnemonics for the no-operand form.
I have a simple loop that takes a name and prints the name without saving it.
looptext getc ;starts text get loop for name
;since name isn't re-used, we don't have to save it
add r1, r0, -10 ;Test for enter character
brz finishloop1 ;if enter, cancel the text loop
OUT ;If it's not enter, print out the character typed
br looptext ;Go back to loop
finishloop1
The program then asks for an ID number separated by spaces. All these values are saved into an array and each loop, it checks if the new input is the 'new' lowest value or 'new' highest value and sets it into the respectable register.
[Deleted code for copyright sake]
At the end of the code, where I need to add a sorting algorithm, I am left with an array of only characters.
I need to go through each index of the array and rearrange the characters in the numerical order (smallest to largest).
Thank you very much all of you for the tips and tricks. Thank you specifically #Ped7g for linking me that sorting algorithms page. I ended up searching around and actually finding someone on gitub that had a bubble algorithm already written out in Assembly code. So thanks for indirectly giving me the answer.
Note: For any future people coming here to find answer, here is the link the bubble sorting algorithm code:
https://github.com/oc-cs360/s2014/blob/master/lc3/bubblesort.asm. This is part of the lecture notes for a university course.
; Implementing bubble sort algorithm
; R0 File item
; R1 File item
; R2 Work variable
; R3 File pointer
; R4 Outer loop counter
; R5 Inner loop counter
.ORIG x3000
; Count the number of items to be sorted and store the value in R7
AND R2, R2, #0 ; Initialize R2 <- 0 (counter)
LD R3, FILE ; Put file pointer into R3
COUNT LDR R0, R3, #0 ; Put next file item into R0
BRZ END_COUNT ; Loop until file item is 0
ADD R3, R3, #1 ; Increment file pointer
ADD R2, R2, #1 ; Increment counter
BRNZP COUNT ; Counter loop
END_COUNT ADD R4, R2, #0 ; Store total items in R4 (outer loop count)
BRZ SORTED ; Empty file
; Do the bubble sort
OUTERLOOP ADD R4, R4, #-1 ; loop n - 1 times
BRNZ SORTED ; Looping complete, exit
ADD R5, R4, #0 ; Initialize inner loop counter to outer
LD R3, FILE ; Set file pointer to beginning of file
INNERLOOP LDR R0, R3, #0 ; Get item at file pointer
LDR R1, R3, #1 ; Get next item
NOT R2, R1 ; Negate ...
ADD R2, R2, #1 ; ... next item
ADD R2, R0, R2 ; swap = item - next item
BRNZ SWAPPED ; Don't swap if in order (item <= next item)
STR R1, R3, #0 ; Perform ...
STR R0, R3, #1 ; ... swap
SWAPPED ADD R3, R3, #1 ; Increment file pointer
ADD R5, R5, #-1 ; Decrement inner loop counter
BRP INNERLOOP ; End of inner loop
BRNZP OUTERLOOP ; End of outer loop
SORTED HALT
FILE .FILL x3500 ; File location
.END
I am having a difficult time understanding this particular problem. I have the answers, but I really want to know the reason as to why they are what they are! I understand how each opcode works, just not in applying it to this problem.....
An engineer is in the process of debugging a program she has written. She is looking at the following segment of the program, and decides to place a breakpoint in memory at location 0xA404. Starting with the PC = 0xA400, she initializes all the registers to zero and runs the program until the breakpoint is encountered.
Code Segment:
0xA400 THIS1 LEA R0, THIS1
0xA401 THIS2 LD R1, THIS2
0xA402 THIS3 LDI R2, THIS5
0xA403 THIS4 LDR R3, R0, #2
0xA404 THIS5 .FILL xA400
Show the contents of the register file (in hexadecimal) when the breakpoint is encountered.
Again, I'm not seeking a list of answers, but an explanation to help me understand what exactly is going on in the program. Thanks so much!
If the engineer put the breakpoint on line 0xa404 (stopping the program before 0xa404 is run), the code would do the following:
0xA400 THIS1 LEA R0, THIS1 ; LEA loads the address of THIS1 into R0.
; Since THIS1 is at memory location 0xA400,
; after this instruction R0 = 0xA400
0xA401 THIS2 LD R1, THIS2 ; LD loads the contents of the memory at
; THIS2 into R1. Since THIS2 is this very
; line its contents are this instruction,
; which is 0010001111111111 in binary or
; 0x23ff in hex, so after this line executes
; R1 hold 0x23ff
0xA402 THIS3 LDI R2, THIS5 ; LDI visits THIS5 and treats its value as a
; new memory location to visit. It visits
; that second location and stores its
; contents into R2. In this case, it would
; look at THIS5 and see its value is 0xA400.
; It would then visit 0xA400 and store its
; contents in R2. 0xA400 contains the first
; line of your program which translates to
; 1110000111111111 in binary, 0xe1ff in
; hex, so it stores 0xe1ff into R2.
0xA403 THIS4 LDR R3, R0, #2 ; LDR starts from the memory location of R0,
; adds 2 to that, then stores whatever it
; finds in that memory location into R3. In
; this case R0 = 0xA400. It adds 2, bringing
; it up to 0xA402, which is the instruction
; immediately above this one. In binary, that
; instruction is 1010 0100 0000 0001, which
; translates into 0xa401 so the program stores
; the program stores 0xa401 into R3.
0xA404 THIS5 .FILL xA400
I'm beginning in ARM assembly and I've been trying to write a simple integer division subroutine. So far, I have the following:
.text
start:
mov r0, #25
mov r1, #5
bl divide
b stop
divide:
cmp r0, r1
it lo
mov pc, lr
sub r0, r0, r1
add r2, r2, #1
b divide
stop:
b stop
I wrote it based on the pseudocode I came up with for the algorithm:
Is the Divisor (bottom) larger than the Dividend (top)?
Yes:
-Return the remainder and the counter(quotient)
No:
-Subtract the Divisor from the Dividend
-Increment the counter by 1
-Repeat the method
r0 contains the numerator and r1 contains the denominator.
When the algorithm finishes, r0 should contain the remainder and r2 should contain the quotient. However, upon running, r0 contains 19 and r2 contains 0.
Are there any fallacies in my logic that I'm just missing?
I removed it lo and changed mov to movlo and it worked fine.