Learn to Code via Tutorials on Repl.it!

← Back to all posts
Assembly Language Crash Course 2
Wuru (539)

Third take.

Hello. I am @Wuru and this is Assembly Language Crash Course 2.

So. Let’s just start right now.

Section 4: Hello, World!

Consider the code below.

section .text
  global _start
_start:
  mov eax, 4
  mov ebx, 1
  mov ecx, msg
  mov edx, len
  int 0x80
section .data
  msg db “Hello, World!”, 0x0a
  len equ $ - msg

The code above prints ‘Hello, World!’ to the terminal.

How?

Well, let’s break down the parts you may not understand.

You can see via int 0x80 we are requesting a kernel interrupt. We discussed this in section 2.

The interruption 0x80 uses eax as a code to determine what to do. We discussed this in section 2.

eax contains 4. You can see this via mov eax, 4

4 is the code for a sys_write. This means the computer is going to write data somewhere.

The sys_write interrupt is also dependent on ebx for the destination to write.

A better explanation would be that ebx is the file descriptor. (0 is stdin, 1 is stdout, and 2 is stderr, respectively.)

These codes are the same in C.

Do I expect you to understand these codes? No. But reference them in the future.

As you can see via mov ebx, 1 that 1 is going into ebx.

1 is the code for stdout. STDOUT is the output stream meaning that the computer will write something to the terminal window.

You might recognize this from C or C++.

sys_write also depends on ecx and edx.

But why?

ecx needs to contain the data.

And edx needs to contain the amount of data that will be written.

And this is where the data section comes into play.

As you can see in the data section I defined msg. For reference, see section 3.

This is moved into ecx via mov ecx, msg. This is possible because (as seen in section 3) pointers just point to an address in memory.

So this is just like moving that address into ecx. Just a bit abstracted.

Also in the data section there is len.

len is a pointer to another address in memory.

The equ keyword (after len) is a keyword to say that it is going to equal something.

$ is the end of msg.

And msg is the start.

So to find the length of msg we can just subtract.

And that’s it!

Section 5: labels and jmp

Consider the code below

global _start
other:
  mov eax, 1
  mov ebx, 0
  int 0x80
_start:
  jmp other

The code will just end itself.

How?

Well let’s break it down.

The _start label is a label we have been using since the start. pun intended

And other is just another label we defined.

As you can see in other we perform a system interruption to end the program. As seen in section 2.

And jmp jumps to another label.

As you can see we jump to other.

That’s pretty much it.

Farewell until part 3!

Please upvote and give feedback :D!

Commentshotnewtop
Verbix (2)

he at the Ws again

Highwayman (1362)

My only regret for this one is that you don’t go deeper into jmps like jne and stuff. That’s where I really got excited when I first started learning cause I was like YEAS! BRANCHING!

Highwayman (1362)

@Wuru oh ok ye makes sense :)

HahaYes (1014)

Pretty good! I will upvote because I'm not competing in the tutorial section! (currently)

firefish (305)

@Wuru I don't know AT&T syntax for assembler. GNU Syntax only please.

Wuru (539)

This is intel, same as GNU. @johnstev111

Wuru (539)

Also don’t forget to upvote. @johnstev111

firefish (305)

@Wuru GNU swaps the operands and uses % signs and $ signs Hrmph.

Wuru (539)

That’s AT&T syntax aswell. @johnstev111

firefish (305)

@Wuru They're different!
this is gnu assembly:

.section .text
  .global main
  main:
    movq $0x6FE42A040FB4EB02, %rcx
    movb $0xEA, %al
    cmpb %ch, %al
    jna rnn
  eldak:
    incb %ch
    retq
  rnn:
    movq $1, %rax
    movq $1, %rdi 
    jmp eldak

try converting thata to AT&T

Wuru (539)

What I meant is this is intel. Not AT&T. @johnstev111

firefish (305)

@Wuru intel syntax is AT&T syntax, GAS syntax is seperate

CodeLongAndPros (952)

Nice, but I found two inconsistencies:

performing a system interruption.

I believe it should be 'requesting a kernel interrupt'.

And:

As you can see via mov ebx, 1 that 1 is going into ebx.
1 is the code for stdout. STDOUT is the output stream meaning that the computer will write something to the terminal window

I think it should be:

ebx is the file descriptor for the write. (A descriptor of 0, 1, or 2 is a special one, indicating stdin, stdout, or stderr, respectively.

Wuru (539)

Your first request will be fulfilled. Your second one, while it is more accurate, is too complicated for the tone I’m going for. @CodeLongAndPros

CodeLongAndPros (952)

@Wuru I just feel that you should at least give a little bit of info on what ebx is, and, plus, most people reading this already know how to program.

Wuru (539)

I will try to go into more detail, but only for an upvote. ;) @CodeLongAndPros

Wuru (539)

I also said: ‘ The sys_write interrupt is also dependent on ebx for the destination to write.’ @CodeLongAndPros

CodeLongAndPros (952)

@Wuru You could also add that the destination codes are the same as in C.

Wuru (539)

As soon as upvote begging starts @CodeLongAndPros leaves.

NDLFOREVER (288)

T pose to assert.h

Highwayman (1362)

@NDLFOREVER lol.
Ya know in c++ we don’t have to rely on others to assert our dominance.

static_assert(1,"my dominance");

One of the many advantages of c++ over c... XD

Wuru (539)

Now to assert my dominance imma force you to upvote. @Highwayman

NDLFOREVER (288)

ah no yes everything makes no total sense about the code at the beggining I don't even have to read down all I needed was that first image before realizing that I aredy didn't  knew this haha definitlty not

Wuru (539)

Read part 1 for context. @NDLFOREVER

NDLFOREVER (288)

ik i was just joking

DynamicSquid (2611)

Just a quick question, does a compiled language translates code into assembly?

Wuru (539)

Most likely. Unless it is a really high-level language in that case it would translate to C or C++ and then assembly. But yes, most languages translate down to assembly. @DynamicSquid

DynamicSquid (2611)

@Wuru so the "higher" a language is, the more distant it is from Assembly? Like a really high language would translate down to a lower language, and to a even lower language, and then Assembly?

Wuru (539)

Yes the higher a language is the further it is from assembly. @DynamicSquid

mwilki7 (1012)

@DynamicSquid
even weirder most of the assembly we see is even too high level for the processor (goes even to a lower level of assembly)

Wuru (539)

Don’t forget to upvote :D self promo @mwilki7

CodeLongAndPros (952)

@Wuru The higher is is is simply the abstraction level. You can have low level libaries in a high level language and vice versa

Wuru (539)

Yes, yes I know but those libraries are added at the correct time in compilation. For example, if had a language that compiled to C++ and then assembly and you had a C++ library written for it. It would incorporate during the C++ stage. @CodeLongAndPros

CodeLongAndPros (952)

@Wuru I am not questioning the runtime processes, but high-level does not mean 'far from assembly'.

It can mean far from machine code, but it can also mean other things.

A high level library could implement the Noti object.

Instead of calling every method and nitpicking over cross platform details, you could call Noti.notify("Foo")

Wuru (539)

Let me reiterate. It means the more assembly code you need for each line of source code. @CodeLongAndPros

CodeLongAndPros (952)

@Wuru Wait what?

So the an if .. then group in C is lower-level than a printf?

Wuru (539)

No. I was restating. It has nothing to do with level anymore. @CodeLongAndPros

Highwayman (1362)

@Wuru @CodeLongAndPros @DynamicSquid ya know there was a really nice explanation I got from a book about a month ago on the evolution from a low to high level language, and they were directly relating it to abstraction. Let me explain.

As we go up the ladder of abstraction, we are slowly stripping away constantly repeated concepts/paradigms and replacing them with a more simplistic version or with nothing at all and handle the construction of those paradigms on instead of the programmer. So a high level language is really just a language that has a ton of abstraction. In the same exact sense a high level library just has a bunch of abstraction. The amount of abstraction is key to figuring out how high level a language is. Maybe I’ll send you guys the book, it’s so good! :)

Highwayman (1362)

@CodeLongAndPros Ye! Right? Ok I’m go get the book name it’s on github...

CodeLongAndPros (952)

@Highwayman It's kinda like reading the Halloween Documents.

Highwayman (1362)

@CodeLongAndPros alright it’s called
"Operating Systems from 0 to 1" it’s on github if you just google it’s like the top thing

CodeLongAndPros (952)

@Highwayman
Internal M$ documents relating to Linux
http://www.catb.org/~esr/halloween/

(Huh, last time I read this, I was playing an online game over data because my power was out. Fun times)

Highwayman (1362)

@CodeLongAndPros holy crap I gotta read this stuff like now thank you so much for sharing this is sick! :D

Highwayman (1362)

@CodeLongAndPros ye :P this'll be fun lol.

normal ppl: oof kayne west wants to run for pres
programmers: oof someone leaked windows docs

lol

Highwayman (1362)

@Wuru a lot of pings would be like 60. we've only had like 5 pings lol.

CodeLongAndPros (952)

@Highwayman So true. But those were from 2000.

Highwayman (1362)

@CodeLongAndPros true true.. I just couldn't think of anything else to use that was good enough that was recent and I'm all over the place rn so I was just like "this"

CodeLongAndPros (952)

@Highwayman

Linus sharing his PC build

FTFY

DynamicSquid (2611)

@Highwayman just got on with 43 notifications in 1 hour lol

Wuru (539)

Just came back after 1hr to 7 :/ @Highwayman

firefish (305)

@Highwayman huh? why so many question marks? {aoz}

Highwayman (1362)

Bc I have no idea what that means( and I was just increasing the amount by one each time) @johnstev111