Inner and outer loop in ARM assembly - arm

I am new to ARM assembly and need to create a program that prints out 5 lines using an inner and outer loop. The inner loop counts backwards from f in hex all the way down to 0, and the outer loop runs the inner loop 5 times. So it will print out f e d c b a 9 8 7 6 5 4 3 2 1 0 5 times, each iteration on a different line. I do not know how to do this and any help will be appreciated.
I only know how to print strings, and I need to use printf for this program which I also do not know how to use.

Related

Python scope and loop confusing

I am new to python.
May I ask why the sixth outcome is 8 instead of 5? As I learnt from "scope" that the later statement should not be affected by whatever happened in another inner scope, so i+=3 should have no effect on what "i" is going to be printed? Thank you for the help.
for i in range (0,10):
if i==5:
i+=3
print i
outcome:
0
1
2
3
4
8
6
7
8
9
In the code you created a condition that if the i reaches number 5 it will add +3 giving an 8.
the += adds do not replace.
if you expect that change the number 5 with a 3 try:
for i in range (0,10):
if i==5:
i = 3
print i

recursion - Not sure why the numbers increase and then decrease 1234554321

I am currently learning the basics of C, and I came across this code. The main function calls Count() and passes 1. And in Count(), 1 is printed and since cnt is less than 5 it'll keep on calling itself and therefore continue to print out 1 2 3 4 5.
After, cnt is 5, then it'll print out 5 and then since cnt is = 5, so it'll print out 5 again (won't satisfy the if condition statement).
void Count(int cnt){
printf("%d\n", cnt);
if(cnt < 5){
Count(cnt + 1);
}
printf("%d\n", cnt);
}
int main(){
Count(1);
}
I thought the output would simply be 1 2 3 4 5 5, but I don't get why its 1 2 3 4 5 5 4 3 2 1.
An easy-to-understand explanation would be appreciated!
First thing to notice is that you have two printf statements in each count function. So you will print the passed number cnt two times in the function.
The second thing you need to know is how the functions are called.
count(1) -> count(2) -> count(3) -> count(4) -> count(5) At count(5) the function will not call any further counts.
Up to here you have printed 1 2 3 4 5
The return will be count(5) -> count (4) -> count (3) -> count(2) -> count (1)
Before each return you will print respectively 5 4 3 2 1
Finally you get
1 2 3 4 5 5 4 3 2 1
The first printf() prints all of the numbers counting up, as each new call to Count() adds another call frame to the stack. This prints
1
2
3
4
5
Once the if condition (which makes the recursion continue) is no longer met, no more recursive calls are made, which allows the recursion to end and the last printf() (located after the if condition) for the most recent stack frame now runs. This prints the 2nd 5. The stack frames on the stack now "unroll", from most recent to least recent, allowing each function call/stack frame to run to completion and thereby perform it's 2nd printf() statement, which now comes out in reverse order (including the 5 I just mentioned above) as:
5
4
3
2
1
Remember that recursion adds stack frames for a given function call, and only after recursion stops going deeper (the end condition is met) does the latter part of the function, after the recursive call to Count(), for each stack frame, now in reverse order on the stack, begin to get run to completion, releasing each stack frame from the stack as each recursive function call on the stack runs through to the end of the function.
Answered from my phone. Forgive the lack of formatting.
Fixed!

Find elements in array those have come two times in array

Given A = [3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 8]
Output B = [3 4 5 6 8]
Is there a Matlab function or command to get this result? I am new to Matlab. Just now I am doing it going through for each element and keeping a counter for it. I have very big array so this is taking too much time.
Use a combination of unique and histc:
uA = unique(A); %// find unique values
B = uA(histc(A, uA)>=2); %// select those that appear at least twice
The above code gives the values that appear at least twice. If you want values that appear exactly twice, replace >= by ==.

Matrix line and column seem to be printed inverted in Fortran

I have a simple question about arrays of 2 dimensions in Fortran 95 (i.e., matrices). By what I know, mathematics define an element inside of a matrix as Aij, where i represents its line and j its column. Well, if I simply code write(*,*) Matrix, the result has lines and columns inverted! Take the following example code:
program TEST
implicit none
integer :: P(3,3), i
P(1,1)=1
P(1,2)=2
P(1,3)=3
P(2,1)=4
P(2,2)=5
P(2,3)=6
P(3,1)=7
P(3,2)=8
P(3,3)=9
do i=1,3
write(*,"(3(I1,1X))") P(i,1:3)
enddo
write(*,*)
write(*,"(3(I1,1X))") P
end program TEST
By using the loop above (which fixes a line and then print each column inside of it), I get the result I expected:
1 2 3
4 5 6
7 8 9
Now by using the last statement write(*,"(3(I1,1X))") P, I get:
1 4 7
2 5 8
3 6 9
Am I doing something wrong here?
When you output an entire array as an entity, Fortran outputs the array in its internal memory layout. What you are seeing is that Fortran is a column-major language. See http://en.wikipedia.org/wiki/Row-major_order#Column-major_order

Problem with "for" and/or "if, then" statement(s)

Here is a seemingly simple function to generate prime numbers. However, it does not work as expected. I have trawled the online guides but I can't seem to get my head around it. Your help is much appreciated.
primes = function(limit)
local t = {2}
for i = 3, math.sqrt(limit) do
for k, v in ipairs(t) do
if i % v == 0 then -- check if "i" is evenly divisible by each table element
break
else table.insert(t, i) -- if it is not, it is a prime number
break
end
end
end
return t
end
When I do:
for k, v in ipairs(primes(15)) do print (k, v) end
I get:
1 2
2 3
3 5
4 7
5 9
6 11
7 13
8 15
9 and 15 are not prime numbers, and it looks like the second "for" loop is not going past the first element in the table(2). What is the correct way to use the "for" loop in this case?
Thanks, Vince
EDIT: limited the passed in argument to its square root as suggested.
You're inserting too soon, you have to finish the for loop before you do the insert. Here's one way:
primes = function(limit)
local t = {2}
local is_prime, i_rt
for i = 3, limit do
is_prime=true
i_rt=math.sqrt(i)
for k, v in ipairs(t) do
if i % v == 0 then -- evenly divisible, not prime
is_prime=false
break
end
if v > i_rt then -- break once you exceed square root of i for efficiency
break
end
end
if is_prime then
table.insert(t, i) -- insert if it is a prime
end
end
return t
end
And an example:
> for k, v in ipairs(primes(50)) do print (k, v) end
1 2
2 3
3 5
4 7
5 11
6 13
7 17
8 19
9 23
10 29
11 31
12 37
13 41
14 43
15 47
I think you just need to flip the order of your for loops. As it is, you're testing 3 against every number, then four against every number, then five against every number, and so on. If you switch your two "for" statements, you'll compare 3,4,5... against the first number, 3,4,5... against the second number, 3,4,5... against the third number and so on.
EDIT
Actually, you'll have to do a bit more. You have to make sure that none of 3,4,5... divide the number, and then after the inner for loop, insert the number if nothing has gone in. Furthermore, you should limit your inner loop to stop at sqrt(v), because if nothing under the sqrt(v) divides v, then nothing over sqrt(v) will either (besides v).
EDIT
Actually, I think I misinterpreted your code, and you should ignore what I said. Limit the inner loop to sqrt, but other than that, go with what BMitch said.

Resources