Assembly Language

Zach_7

Junior Member
Joined
Apr 20, 2020
Messages
2
Reaction score
0
Can someone help me with this?

1.Re-write the following C function, to help the ARM compiler produce the most efficient assembly code possible:

int iter_mul(int n)
{
int i, a = 0, b = 0;

for (i = 0; i < 4; i++)
{
a += 5 * n;
b += 3 * n;
}

return (a + b);
}

2. Write the following code in ARM assembly, by ensuring optimal execution speed and code density(minimum number of lines/instructions). Comment each line of your assembly code.

while (a > b || a < b)
{
if a > b
a--;
if a < 8
a++;
}
c = 5 * a;





3. Describe the following ARM assembly code, by describing the function of each instruction and the overall logic flow of the code. Which C instruction/structure does this code describe?

CMP r0, #8
ADDLT pc, pc, r0, LSL#2
B ld
B l0
B l1
B l2
B l3
B l4
B l5
B l6
B l7
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Can someone help me with this?

1.Re-write the following C function, to help the ARM compiler produce the most efficient assembly code possible:

int iter_mul(int n)
{
int i, a = 0, b = 0;

for (i = 0; i < 4; i++)
{
a += 5 * n;
b += 3 * n;
}

return (a + b);
}

...

I will only do one to show you, I don't need to care if it will be fast on ARM, it will be faster on any architecture (I claimed, you can confirm)

Code:
inline int iter_mul(int n) __attribute__((always_inline));
int iter_mul(int n) {
  return n << 5;
}
 
Last edited:

Zach_7

Junior Member
Joined
Apr 20, 2020
Messages
2
Reaction score
0
Anyway thanks buddy. It just that im weak in assembly language. thats the reason i need some guidance
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Anyway thanks buddy. It just that im weak in assembly language. thats the reason i need some guidance

Any language being the same, ur best optimisation comes from the source. Assembler optimisation always come after u have first optimise ur main language first.

Well even if you are weak, you should help yourself first, not just blindly post ur questions here and expect answers. Put down what you need as guidance on ur attempts. :)
 

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,719
Reaction score
529
Any language being the same, ur best optimisation comes from the source. Assembler optimisation always come after u have first optimise ur main language first.

Well even if you are weak, you should help yourself first, not just blindly post ur questions here and expect answers. Put down what you need as guidance on ur attempts. :)
If don't understand assembly language how to understand and trace the program flow in OllyDog?:( This dog only speak assembly language. :o
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
If don't understand assembly language how to understand and trace the program flow in OllyDog?:( This dog only speak assembly language. :o

Assembly language is not difficult, just tedious. The core knowledge to know is not the language, but the system architecture concepts. How memory are fetched, how they are written, what do the mnemonic code means and how opcodes works, how DMA works, how the processor works on whole, etc. Obviously the more in-depth u know about processors and all the surround topics, the better you are at reading mnemonic codes, because they basically translate to direct instructing ur processors what to do.

mapping from mnemonic codes back into high level programming languages constructs becomes much more difficult because it is much harder to understand variable scoping, methodologies from just merely basic building blocks of mnemonic codes. All you see is registers, stack, memory, references, and interrupts at play.

I pick up assembly and processors understand from MIPS, but applying them to x86 is not all that different. :)
 

Trader11

Banned
Joined
Oct 14, 2018
Messages
15,698
Reaction score
5,233
Assembly language is not difficult, just tedious. The core knowledge to know is not the language, but the system architecture concepts. How memory are fetched, how they are written, what do the mnemonic code means and how opcodes works, how DMA works, how the processor works on whole, etc. Obviously the more in-depth u know about processors and all the surround topics, the better you are at reading mnemonic codes, because they basically translate to direct instructing ur processors what to do.

mapping from mnemonic codes back into high level programming languages constructs becomes much more difficult because it is much harder to understand variable scoping, methodologies from just merely basic building blocks of mnemonic codes. All you see is registers, stack, memory, references, and interrupts at play.

I pick up assembly and processors understand from MIPS, but applying them to x86 is not all that different. :)

Does it apply to Arm?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Does it apply to Arm?

Applies to all low construct instructions. Assembly language of processors or even microcontrollers are not the lowest level. There are still opcodes below assembly language. Assembly language are still largely human readable mnemonic codes that you can easily identify what it does. Example, LD IS LOAD. ADDLT is ADD SOMETHING TO SOMETHING IF CONDITION IS LESS THAN, or. JZ is JUMP TO LABEL OF ZERO FLAG IS SET. I am mixing assembly mnemonic codes from various architecture.

Generally it doesn’t matter, all these architectures may differs in a lot of ways but generally follows well understood machine concepts discovered decades ago. SIMD, DMA, STACK MACHINE vs REGISTER, RISC vs CISC, are yesterday concepts.

ARM didn’t discover their architecture from scratch. Even at assembly level, you are still not exposed into how processors works. Like how predictive branching works, how registers renaming works inside the processor, how single opcodes are split into multiple micro opcodes to create more overlapping concurrency when executing a sequence of opcodes where each may have to wait for different parts of the processors for the execution to be completed. Compilers and/or Processors also does instructions reordering if there are no calculated dependency among the instructions.

Are all these learnt because of Assembly language? Definitely NO. But do you need to know all these to be good in Assembly language? Definitely YES. Your assembly language are just instructions, what you are trying to drive is a machine. The performance you want to get from is a machine, the machine is what you want to understand. You can’t get better just simply knowing assembly language, when you don’t understand memory wait states and what it means between the processor and memory when they don’t work on the same clockspeed. Why is the memory clockspeed different from the processor? How does reading from the memory works? What are address lines and what are data lines? How do the signal of the memory get READ or WRITTEN? Do you need to know abit of how electrical signals works between electronic components?

Suddenly you will think I am over exaggerating on what you need to know but clearly all these seems not described in the languages right? Well that is the difference between a piece of hack and just another piece of instructions. Software engineers of the previous era designing for games consoles knows their machine so well that they can come out with hacks and techniques to exploit performance out from the machine, even to the point of taking advantages of milliseconds screen refresh to run computation when the screen is not drawing. Now because the graphic world is so complex that programmers just simply throw an OPENGL instruction and expect the drivers and the hardware to do all the rest for good performance.

I hope this passage drill more into understanding Computing and Computer Science and open up to more questions of what does a Computer Scientist means to you. Just a software developer? or perhaps it is not the title that means something, but knowing the vast knowledge behind just a programming language that makes you who you are :)
 

Trader11

Banned
Joined
Oct 14, 2018
Messages
15,698
Reaction score
5,233
Applies to all low construct instructions. Assembly language of processors or even microcontrollers are not the lowest level. There are still opcodes below assembly language. Assembly language are still largely human readable mnemonic codes that you can easily identify what it does. Example, LD IS LOAD. ADDLT is ADD SOMETHING TO SOMETHING IF CONDITION IS LESS THAN, or. JZ is JUMP TO LABEL OF ZERO FLAG IS SET. I am mixing assembly mnemonic codes from various architecture.

Generally it doesn’t matter, all these architectures may differs in a lot of ways but generally follows well understood machine concepts discovered decades ago. SIMD, DMA, STACK MACHINE vs REGISTER, RISC vs CISC, are yesterday concepts.

ARM didn’t discover their architecture from scratch. Even at assembly level, you are still not exposed into how processors works. Like how predictive branching works, how registers renaming works inside the processor, how single opcodes are split into multiple micro opcodes to create more overlapping concurrency when executing a sequence of opcodes where each may have to wait for different parts of the processors for the execution to be completed. Compilers and/or Processors also does instructions reordering if there are no calculated dependency among the instructions.

Are all these learnt because of Assembly language? Definitely NO. But do you need to know all these to be good in Assembly language? Definitely YES. Your assembly language are just instructions, what you are trying to drive is a machine. The performance you want to get from is a machine, the machine is what you want to understand. You can’t get better just simply knowing assembly language, when you don’t understand memory wait states and what it means between the processor and memory when they don’t work on the same clockspeed. Why is the memory clockspeed different from the processor? How does reading from the memory works? What are address lines and what are data lines? How do the signal of the memory get READ or WRITTEN? Do you need to know abit of how electrical signals works between electronic components?

Suddenly you will think I am over exaggerating on what you need to know but clearly all these seems not described in the languages right? Well that is the difference between a piece of hack and just another piece of instructions. Software engineers of the previous era designing for games consoles knows their machine so well that they can come out with hacks and techniques to exploit performance out from the machine, even to the point of taking advantages of milliseconds screen refresh to run computation when the screen is not drawing. Now because the graphic world is so complex that programmers just simply throw an OPENGL instruction and expect the drivers and the hardware to do all the rest for good performance.

I hope this passage drill more into understanding Computing and Computer Science and open up to more questions of what does a Computer Scientist means to you. Just a software developer? or perhaps it is not the title that means something, but knowing the vast knowledge behind just a programming language that makes you who you are :)

But most NUS computer science students are not so pro with this Assembly Language and low level thinking....so how to learn by yourself?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
But most NUS computer science students are not so pro with this Assembly Language and low level thinking....so how to learn by yourself?

Why do you care if most of whatever CS students knows or not, it only matters if you know or not. There are 1001 things not taught extensively in any institution. It takes decades to master a discipline. Any institutions only scratches the surface. For Computer Science is a combination of Mathematics, Sciences and Philosophies, do you expect a 3-4 year course can reach you everything? Obviously you need to learn on yourself, reading and experimenting with all sorts of things.

A comprehensive CS course will most certainly introduce you to Computer Architecture, OS. On my own, I took up courses that teaches Programming Languages design, Compiler courses, Knowledge Based system, Computer Graphics courses, Parallel and Concurrent programming courses to equip myself with advance computing knowledge. Outside of school, I play with makers tools like SBC, cloud computing, JVM core topics like Garbage Collection. During my JC days learning Turbo Pascal, where computers are mostly running in real mode instead of the new protected mode now, I have picked up knowledge on my own mixing assembly language with Pascal, directly accessing video memory, BIOS interrupts to read key codes, not ASCII codes, play with timer interrupts to run real-time events outside of the main execution(no threading ideas back then), creating my new “database” using my own implementation of file-based indexed(hash) tables. Playing with memory overlay to allow dynamic paging of codes.

I am afraid most developers these days won’t be able to get that low, not because they can’t, but the circumstances doesn’t encourage them to invent the wheel. Everyone talk about SDK. Not much of such things in the past where you simply use a library, hence much lesser of such opportunities to sharpen your skill sets.

Invention exists because of circumstances, if you don’t have a need, you wouldn’t need to create it. Hence the only way moving forward is create the needs yourself as a challenge. :)
 

Trader11

Banned
Joined
Oct 14, 2018
Messages
15,698
Reaction score
5,233
Why do you care if most of whatever CS students knows or not, it only matters if you know or not. There are 1001 things not taught extensively in any institution. It takes decades to master a discipline. Any institutions only scratches the surface. For Computer Science is a combination of Mathematics, Sciences and Philosophies, do you expect a 3-4 year course can reach you everything? Obviously you need to learn on yourself, reading and experimenting with all sorts of things.

A comprehensive CS course will most certainly introduce you to Computer Architecture, OS. On my own, I took up courses that teaches Programming Languages design, Compiler courses, Knowledge Based system, Computer Graphics courses, Parallel and Concurrent programming courses to equip myself with advance computing knowledge. Outside of school, I play with makers tools like SBC, cloud computing, JVM core topics like Garbage Collection. During my JC days learning Turbo Pascal, where computers are mostly running in real mode instead of the new protected mode now, I have picked up knowledge on my own mixing assembly language with Pascal, directly accessing video memory, BIOS interrupts to read key codes, not ASCII codes, play with timer interrupts to run real-time events outside of the main execution(no threading ideas back then), creating my new “database” using my own implementation of file-based indexed(hash) tables. Playing with memory overlay to allow dynamic paging of codes.

I am afraid most developers these days won’t be able to get that low, not because they can’t, but the circumstances doesn’t encourage them to invent the wheel. Everyone talk about SDK. Not much of such things in the past where you simply use a library, hence much lesser of such opportunities to sharpen your skill sets.

Invention exists because of circumstances, if you don’t have a need, you wouldn’t need to create it. Hence the only way moving forward is create the needs yourself as a challenge. :)

How do you find the time to learn these on your own? And no one is paying for you to learn these theories?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
How do you find the time to learn these on your own? And no one is paying for you to learn these theories?

Time? Do you have a lack of time in your life? For example, for the time you spent asking me this question, reading numerous other threads, would those time no be enough for you to read an article about ARM assembly language?

Do you mean you are so busy 24 by 7 where you can’t spare 2 good hours across the entire 24 hrs reading useful information on the Internet? Weekends, are those time use to listen to music, watch netflix, play games where they could be spent reading an ebook on anything from Understand RDBMS or NOSQL, or understanding cloud computing services, knowing which are the latest features in your favourite programming language, or simply have some deep thoughts about a recent attempt u made on ur assignments and think if it could have been done differently or even better architect differently?

Time is always available for most people. It is a matter on what is your focus and how you spend it wisely.

Is knowledge not valuable itself? Is knowledge not necessarily in all your endeavours whether they are work related or not. Does it not require investment in your own knowledge? Books and reading materials have all the answers you need, what you need to learn to put them together with your own initiative, your own research, your own diligence. Do it often enough and you quickly realise you simply just can’t stop thinking about it.

For me, something totally unrelated like watching a 3D cartoon, makes me think about raytracing, makes me think about how it is done so efficiently, makes me think about if a recent work on parallelism can be done more effectively if there are such a tool out there that can help me, and the next thing is I just simply search on such keywords in the Internet to see what are done in the open, what others are using. Does it have to end with an objective? Not necessary. Sometimes it is about knowing, that knowledge may have no resultant now, but it can contribute to a question in the future. If you don’t make this investment, you will always have gaps that you can’t fill up. I also think about the universe, about theoretical physics and what it means to our lives. That is why I love star trek and it’s philosophies “To boldly go where no man has gone before” Watch it, watch Startrek TNG, maybe you will find answers to what you are looking for. :)
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top