Page 1 of 1

Who Should Learn Assembly Programming? Part II

#1 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

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

Posted 09 November 2018 - 02:14 AM

This is a follow on from my previous topic found here.

The devil is in the Detail

N.B. I am using Visual Studio Community 2017. All of my examples have been compiled and tested as x64 executable's.

I would strongly recommend you download the Intel manual for 32 and 64 bit architectures found here. This will be your reference manual.

So, continuing with our example but this time in assembly:-

				title		'Test Application'
				public		Main
				include		Windows.inc
				.data
Message			db			'Hello World', 0DH, 0AH, 0
MessageLength	equ			$ - Message
BytesWritten	dq			0
				.code
Main:			sub			rsp, 128
				mov			rcx,  STD_OUTPUT_HANDLE
				call		GetStdHandle
				mov			rcx, rax
				mov			rdx, rsi
while_loop:		mov			al, byte ptr [rsi]
				or			al, al
				je			end_while_loop
				inc			r8
				inc			rsi
				jmp			while_loop
end_while_loop:	lea			r9, BytesWritten
				lea			r9, BytesWritten
				xor			rax, rax
				mov			[rsp+32], rax
				call		WriteFile
				xor			rcx, rcx
				call		ExitProcess
				end



Now, unlike previous tutorials found on this site (and indeed many other sites on the internet), I am not going to go through the assembly code provided above. Instead, I would like you to look at the first tutorial found here, print the last code snippet out and then use that along with the Intel manual to work out what is going on with the assembly code above. Only in this way, do I feel that you will learn the beauty of assembly programming and perhaps improve your programming skills in other higher level languages. Any questions about this code, please do not hesitate to ask and I will try to explain.

Now, back to the code provided above. This code can be refactored as shown below.

				title		'Test Application'
				public		Main
				include		Windows.inc
				.data
Message			db			'Hello World', 0DH, 0AH, 0
MessageLength	equ			$ - Message
BytesWritten	dq			0
				.code
Main:			sub			rsp, 128
				mov			rcx,  STD_OUTPUT_HANDLE
				call		GetStdHandle
				mov			rcx, rax
				lea			rdx, Message
				mov			r8, MessageLength
				lea			r9, BytesWritten
				xor			rax, rax
				mov			[rsp+32], rax
				call		WriteFile
				xor			rcx, rcx
				call		ExitProcess
				end




The windows.inc file.

GetStdHandle proto :qword
WriteFile proto :qword, :qword, :qword, :qword, :qword
ExitProcess proto :qword
STD_OUTPUT_HANDLE equ -11



Null terminated character strings

Assembly does not understand the concept of null terminated strings or escape sequences as in "\n". So in order to handle NULL terminated strings that are used by many of WinAPI functions, you have to compute the length of the string yourself (either using the approach in the first code example, or use the current location counter and subtract the start of the string "MessageLength equ $ - Message".

Labels

Labels in assembly are a necessary evil in all but the simplest of routines. It is important to use meaningful labels to assist others in understanding your code. Back in the early days of computing, severe restrictions, by today's standards, were imposed on the number of characters used in a label (for example in PLAN, the assembly programming language for the ICL 1900 series mainframes, you were restricted to 6 characters). Fortunately modern assembly programmers can use much long labels (I think that MASM imposes a restriction of 255 characters), and that means you can formulate label names that are much more meaningful.

Remember that labels within MASM proc/endp directives, are local to that procedure but do have to be unique to the overall assembly source or program.

This post has been edited by Martyn.Rae: 21 November 2018 - 11:38 PM


Is This A Good Question/Topic? 1
  • +

Page 1 of 1