Commit c52ab0bb authored by Paul Jervis's avatar Paul Jervis

Add assembly examples

parent b44242c1
; find the biggest of two values
; collect an array of two values
values DCD 2, 67
result DCD 0
; set the array addresses to registers
MOV R0, #values
MOV R1, #result
; pull in the first and second elements of the array
; position of the array (R0) with an offset of 0 bytes for the first element
; position of the array (R0) with an offset of 4 bytes for the second element
LDR R2, [R0, #0]
LDR R3, [R0, #4]
; compare the two values
CMP R2, R3
; if R2 is greater, store R2 in address R1
; if R2 is less or equal, store R3 in address R1
STRGT R2, [R1]
STRLE R3, [R1]
; set the results to the memory address of our result (R1) and end the code
; an array of values for dividend and divisor
values DCD 22, 3
; addressses for storing the results
result DCD 0
remainder DCD 0
; put array address into R1
MOV R1, #values
; load the first element from R1 into R0
LDR R0, [R1]
; load the second element (4 bytes...32 bits) higher than the start position in R1
; place the value into R1
LDR R1, [R1, #4]
; store a record of the divisor
MOV R2, R1
; set R3 to 0 as it will store the result
MOV R3, #0
; loop to create a divisor well aligned with the MSB of dividend
; loops until larger then begins the long division
loop
LSL R1, R1, #1
CMP R1, R0
BLE loop
; begin long devision
next
; check to see if the current value of R1 is less than or equal to it's original value
; if it is, end the division
CMP R1, R2
BLE finish
; shift the quotient left by 1 place
LSR R1, R1, #1
; if the remainder of R0 is less than the current value of R1, loop again
; otherwise set the LSB of R3 to 1 and subtract the value of R1 from R0
; then continue the loop
CMP R0, R1
LSL R3, R3, #1
BLT next
ADD R3, R3, #1
SUB R0, R0, R1
B next
finish
; R3 contains the quotient
; R0 contains the remainder
; set these values to the memory addresses we put aside earlier
MOV R4, #result
STR R3, [R4]
MOV R5, #remainder
STR R0, [R5]
END
\ No newline at end of file
; is a value even?
MOV R0, #3
AND R1, R0, #0b1
CMP R1, #0b1
BEQ odd
even
MOV R2, #1
B end_if
odd
MOV R2, #0
end_if
\ No newline at end of file
multiplicand DCD 0b11101010
multiplicator DCD 0b11
MOV R0, #multiplicand
MOV R1, #multiplicator
LDR R0, [R0]
LDR R1, [R1]
MOV R12, #0xFFFFFFFF
MOV R11, #0
;mulitplication
MOV R2, #0
MOV R5, #0
multiply
; get the least significant bit into R3
AND R3, R1, #1
; check to see if the LSB is 0 (R11 is 0)
CMP R3, R11
; R3 == R11, skip to add_zeroes otherwise bitwise AND R0 (the multiplicand)
; with R12, a full compliment of 1s and put the result into R4
BEQ add_zeroes
AND R4, R0, R12
; skip to bit shifting
B shift_bits
add_zeroes
AND R4, R0, R11
shift_bits
; shift all the bits of R1 right by 1 place, this will not loop round
ASR R1, R1, #1
; shift all of the bits of R4 left by as many places as the value of R2
LSL R4, R4, R2
; add the newly shifted value of R4 with the current total in R5
ADD R5, R5, R4
; compare R1 (remaining values to multiply) with R11 (0)
CMP R1, R11
; if R1 == R11, go to finish
BEQ finish
; otherwise if there are still values to multiply by, increase R2 by 1 and loop back to the beginning
ADD R2, R2, #1
B multiply
finish
END
MOV R0, #0
loop
CMP R0, #8
BGE endloop
ADD R0, R0, #1
; Code goes here
B loop
endloop
MOV R10, R0
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment