Page 1 of 1

Who Should Learn Assembly Programming?

#1 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 551
  • View blog
  • Posts: 1,432
  • Joined: 22-August 09

Posted 09 November 2018 - 02:11 AM

Who Should Learn Assembly Programming?

The answer to that question is anybody who calls themselves a programmer and before you all start disagreeing with me, let me explain the reasoning behind this bold statement.

From your perspective, you could argue that in order to drive a car, you don't need to have an in-depth knowledge of how it all works. I would agree totally with you (I have a very basic grasp of the internal combustion engine, clutch and gearing but if it breaks down I wouldn't have a clue!). On the other hand, consider the consequences of breaking down on a busy highway. The embarrassment caused by the chaos that seems to be causing to other motorists and the time wasted because to have to wait for assistance! The costs incurred perhaps to you are minimal but there are costs. Do you not think that having a better understanding of the mechanical and electrical aspects of the vehicle you are driving (these days this is probably a wrong analogy to use as you do need to plug your vehicle into a diagnostics computer to find out what the problem is - unless a wheel has dropped off!).

Having a greater understanding of your code for me anyway is key to writing quality software using any programming language. Assuming that any given set of valid statements in a high level programming language is the only way to implement the required functionality is very narrow minded approach to problem solving. You have to understand the limitations of the code that you are writing particularly if the code compromises the overall effectiveness of the overall software package that said code will be used.

Before We Get Started

If you are reading this tutorial, I can safely assume that you are interested in learning assembly. But before we get going, it is important for you to understand some things about your own programming environment. If you have learnt how to program in an object-oriented programming language, you will find programming in assembly very hard going indeed. This is because you are probably unaware that the 'wool is been pulled over your eyes'. Higher level languages such as C#, VB, Java, Python etc. etc. move you further away from assembly, by building common constructs into the language and reduce the complexities associated with coding in assembly.

Ideally, you need to know how to program in the C programming language as this language can be viewed as one step up from writing in assembly although knowledge of C++ is acceptable if you are prepared to loose the object-oriented aspect.

The 'wool is been pulled over your eyes'

Let's begin by looking at the legendary “Hello World” example.

int main() {
	printf("Hello World\n");
}



As I mentioned in a previous tutorial, the main function is actually a subroutine that is called from the C/C++ housekeeping system. Similarly, printf is a subroutine with the string literal "Hello World\n" as a parameter that outputs the string to the console window. What I am going to do is to show you the code that ends up being executed.

int main() {
#include <windows.h>

int main() {
	char * string = (char *)"Hello World\n";
	DWORD bytesWritten;
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	WriteFile(handle, string, strlen(string), &bytesWritten, NULL); 
}



Before I explain to you what's going on you have to understand that strlen is a C routine that we cannot use in assembly. So we have to modify the code above.

int main() {
#include <windows.h>

int main() {
	char * string = (char *)"Hello World\n";
	DWORD bytesWritten;
	DWORD stringLength;
	char *s = string;
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	stringLength = 0;
	while (*s++ != 0) stringLength++;
	WriteFile(handle, string, stringLength, &bytesWritten, NULL); 
}



We are getting closer but there are a few more things I need to change. Firstly, I need to get rid of the while statement and replace it with a loop (for that is what the while statement is), and I also am going to change the variable names (more about that later).

#include <windows.h>

HANDLE rax;
HANDLE rcx;
char * rdx = (char *)"Hello World\n";
DWORD r8;
DWORD r9;
char *rsi = rdx;
CHAR al;

int main() {
	rcx = reinterpret_cast<HANDLE>(STD_OUTPUT_HANDLE);
	rax = GetStdHandle(reinterpret_cast<DWORD>(rcx));
	rcx = rax;
	r8 = 0;
while_loop:
	al = *rsi;
	if (al == 0) 
		goto end_while_loop;
	r8++;
	rsi++;
	goto while_loop;
end_while_loop:
	WriteFile(rcx, rdx, r8, &r9, NULL); 
}



What we have there is probably the closest that you can get to the number of statements required to complete the task of printing "Hello World\n" to the console in assembly.

... so what was that all that about?

What I have tried to demonstrate is the fact that in order to program in assembly you do not have the niceties associated with the higher level languages. That to many of you is enough reason not to go anywhere near learning how to program in an assembly language.

Yet, strangely enough, programming in assembly is much easier than programming in any high level programming language simply because it is so simple. The vast majority of instructions consist of either a source or a source and a destination. The source has to be either a register, memory location or an immediate operand and the destination has to be a register or a memory location. That said, you cannot specify a source and destination that are both memory locations (all instructions have to involve a register).

Still interested? Fantastic! Please follow this link to continue to the next tutorial.

This post has been edited by Martyn.Rae: 09 November 2018 - 02:17 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Who Should Learn Assembly Programming?

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14576
  • View blog
  • Posts: 58,439
  • Joined: 12-June 08

Posted 10 November 2018 - 08:12 AM

Will this help me "git gud" at SHENZHEN_IO and TIS100 ? :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1