Skip to content

Commit 797b49a

Browse files
authored
Update D_Nasm.md (#74)
minor typo fixes
1 parent c321915 commit 797b49a

1 file changed

Lines changed: 34 additions & 29 deletions

File tree

99_Appendices/D_Nasm.md

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
## Macros
44

5-
There are some cases where some assembly code is preferred/needed to do certain operations (i.e. interrupts handling).
5+
There are some cases where writing some assembly code is preferred/needed to do certain operations (i.e. interrupts handling).
66

7-
Nasm has a macro processor that supports conditional assembly, multi-level file inclusion, etc.
8-
Macros start with the '%' symbol.
7+
Nasm has a macro processor that supports conditional assembly, multi-level file inclusion, etc.
8+
A macro start with the '%' symbol.
99

10-
There are 2 types of macros: single line (defined with %define) and multiline defined with %macro. In this document we will se the multi-line macros.
10+
There are two types of macros: _single line_ (defined with `%define`) and _multiline_ wrapped around `%macro` and `%endmacro`. In this paragraph we will explain the multi-line macros.
1111

1212
A multi-line macro is defined as follows:
1313

@@ -19,7 +19,7 @@ A multi-line macro is defined as follows:
1919
%endmacro
2020
```
2121

22-
In order to be accessible from C the macro has a global label has to be added, so the macro above become:
22+
A macro can be accessed from C if needed, in this case we need to add a global label to it, for example the macro above will become:
2323

2424
```nasm
2525
%macro my_first_macro 1
@@ -33,16 +33,16 @@ my_first_macro_label_%1:
3333

3434
In the code above we can see few new things:
3535

36-
* First we said the the label my_first_macro_label_%1 has to be set as global, this is pretty straightforward to understand
37-
* the %1 in the label definition, let us create different label using the first parameter passed in the macro.
36+
* First we said the the label `my_first_macro_label_%1` has to be set as global, this is pretty straightforward to understand.
37+
* the `%1` in the label definition, let us create different label using the first parameter passed in the macro.
3838

3939
So if now we add a new line with the following code:
4040

4141
```nasm
4242
my_first_macro 42
4343
```
4444

45-
It creates the global label: *my_first_macro_label_42*, since it is global it will be visible also from our C code (of course if the files are linked)
45+
It creates the global label: `my_first_macro_label_42`, and since it is global it will be visible also from our C code (of course if the files are linked)
4646

4747
Basically defining a macro with nasm is similar to use C define statement, these special "instruction" are evaluated by nasm preprocessor, and transformed at compile time.
4848

@@ -55,7 +55,7 @@ my_first_macro_label_42:
5555
sub esp 42
5656
```
5757

58-
## Declaring variables
58+
## Declaring Variables
5959

6060
In Nasm if we want to declare a "variable" initialized we can use the following directives:
6161

@@ -85,7 +85,7 @@ But what if we want to declare a string? Well in this case we can use a differe
8585
string_var:
8686
db "Hello", 10
8787
```
88-
What does it mean? We are simply declaring a variable (string_variable) that starts at 'H', and fill the consecutive bytes with the next letters. But what about the last number? It is just an extra byte, that represents the newline character. So what we are really storing is the string "Hello\\n"
88+
What does it mean? We are simply declaring a variable (string_variable) that starts at 'H', and fill the consecutive bytes with the next letters. But what about the last number? It is just an extra byte, that represents the newline character. So what we are really storing is the string _"Hello\\n"_
8989

9090
Now what we have seen so far is valid for a variable that can be initialized with a value, but what if we don't know the value yet, but we want just to "label" it with a variable name? Well is pretty simple, we have equivalent directives for reserving memory:
9191

@@ -110,12 +110,12 @@ quad_var:
110110
```
111111

112112
One moment! What are those number after the directives? Well it's pretty simple, they indicate how many bytes/word/dword/qword we want to allocate. In the example above:
113-
* *resb 1* Is reserving one byte
114-
* *resw 2* Is reserving 2 words, and each word is 2 bytes each, in total 4 bytes
115-
* *resd 3* Is reserving 3 dwords, again a dword is 4 bytes, in total we have 12 bytes reserved
116-
* *resq 4* Is reserving... well you should know it now...
113+
* `resb 1` Is reserving one byte
114+
* `resw 2` Is reserving 2 words, and each word is 2 bytes each, in total 4 bytes
115+
* `resd 3` Is reserving 3 dwords, again a dword is 4 bytes, in total we have 12 bytes reserved
116+
* `resq 4` Is reserving... well you should know it now...
117117

118-
## Nasm macros from C
118+
## Calling C from Nasm
119119

120120
In the asm code, if in 64bit mode, a call to *cld* is required before calling an external C function.
121121

@@ -141,17 +141,17 @@ call my_c_function
141141
; other magic asm stuff that we don't care of...
142142
```
143143

144-
As mentioned in the multiboot chapter, argument passing from asm to C in 64 bits is little bit different from 32 bits, so the first parameter of a C function is taken from *rdi* (followed by: rsi, rdx, rcx, r8, r9, then the stack), so the *mov rdi, 42* is setting the value of *my_value* parameter to 42.
144+
As mentioned in the multiboot chapter, argument passing from asm to C in 64 bits is little bit different from 32 bits, so the first parameter of a C function is taken from `rdi` (followed by: `rsi`, `rdx`, `rcx`, `r8`, `r9`, then the stack), so the `mov rdi, 42` is setting the value of *my_value* parameter to 42.
145145

146146
The output of the printf will be then:
147147

148148
```
149149
My shiny function called from nasm worth: 42
150150
```
151151

152-
## About sizes
152+
## About Sizes
153153

154-
Variable sizes are alway important while programming, but while programming in asm even more important to understand how they works in assembly, and since there is no real type you can't rely on the variable type.
154+
Variable sizes are always important while coding, but while coding in asm they are even more important to understand how they works in assembly, and since there is no real type you can't rely on the variable type.
155155

156156
The important things to know when dealing with assembly code:
157157

@@ -161,6 +161,7 @@ The important things to know when dealing with assembly code:
161161
mov rax, [memory_location_label]
162162
```
163163
is different from:
164+
164165
```nasm
165166
mov eax, [memory_location_label]
166167
```
@@ -169,10 +170,11 @@ And it could potentially lead to two different values in the register. That beca
169170

170171
This is kind of misleading if we usually do mostly register to memory, or value to register, value to memory, where the size is "implicit".
171172

172-
Probably it can be a trivial issue, but it took me couple of hours to figure it out!
173+
_Authors Note_: Probably it can be a trivial issue, but it took me couple of hours to figure it out!
174+
175+
## If Statement
173176

174-
## If statement
175-
Maybe there are better ways i'm unaware of, but this is a possible solution to a complex if statement, for example if we have the following if we want to translate in C:
177+
Below an example showing a possible solution to a complex if statement. Let's assume that we have the following `if` statement in C and we want to translate in assembly:
176178

177179
```C
178180
if ( var1==SOME_VALUE && var2 == SOME_VALUE2){
@@ -199,7 +201,9 @@ if (var1 == SOME_VALUE || var2 == SOME_VALUE){
199201
//do_something
200202
}
201203
```
202-
it can be implemented in asm in something similar to:
204+
205+
in asm it can be rendered with the following code
206+
203207
```asm
204208
cmp [var1], SOME_VALUE
205209
je .true_branch
@@ -208,7 +212,8 @@ je .true_branch
208212
.true_branch
209213
jne .else_label
210214
```
211-
## Switch statement
215+
216+
## Switch Statement
212217

213218
The usual switch statement in C:
214219
```C
@@ -243,11 +248,11 @@ je .value3_case
243248
;rst of the code
244249
```
245250

246-
## Data structures
251+
## Data Structures
247252

248253
Every language supports accessing data as a raw array of bytes, C provides an abstraction over this in the form of structs. NASM also happens to provide us with an abstraction over raw bytes, that is similar to how C does it.
249254

250-
This guide will just introduce quickly how to define a basic struct,for more information and use cases is better to check the netwide assembler official documentation (see the useful links section)
255+
This guide will just introduce quickly how to define a basic struct, for more information and use cases is better to check the netwide assembler official documentation (see the useful links section)
251256

252257
Let's for example assume we have the following C struct:
253258

@@ -258,8 +263,8 @@ struct task {
258263
};
259264
```
260265

261-
How nasm render a struct is basically declaring a list of offset labels in this way we can use them to access the field starting from the struct memory location (*Authors note: yeah it is a trick...*)
262-
To create a struct in nasm we use the `struc` and `endstruc` keywords, and the fields are defined between these them.
266+
How nasm render a struct is basically declaring a list of offset labels, in this way we can use them to access the field starting from the struct memory location (*Authors note: yeah it is a trick...*)
267+
To create a struct in nasm we use the `struc` and `endstruc` keywords, and the fields are defined between them.
263268
The example above can be rendered in the following way:
264269

265270
```asm
@@ -287,7 +292,7 @@ mov rbx, dword [(rax + task.id)]
287292
```
288293

289294
This is how to access a struct, besically we add the label representing an offset to its base address.
290-
but what if we want to create an instance of it? Well in this case we can use the macros `istruc` and `iend`, and using `at` to access the fields. For example if we want create an instance of task with the values 1 for the id field and "hello123" for the name field, we can use the following syntax:
295+
What if we want to create an instance of it? Well in this case we can use the macros `istruc` and `iend`, and using `at` to access the fields. For example if we want create an instance of task with the values 1 for the id field and "hello123" for the name field, we can use the following syntax:
291296

292297
```asm
293298
istruc task
@@ -296,7 +301,7 @@ istruc task
296301
iend
297302
```
298303

299-
In this way we have declared a struc for the first of the two examples. But again this doesn't work with the second one, because the labels are different. In that case we have to use the full label name (that means adding the prefix task):
304+
In this way we have declared a `struc` for the first of the two examples. But again this doesn't work with the second one, because the labels are different. In that case we have to use the full label name (that means adding the prefix task):
300305

301306
```asm
302307
istruc task

0 commit comments

Comments
 (0)