Learn to Code via Tutorials on Repl.it!

← Back to all posts
Assembly Language Crash Course 5
JustAWalrus (1187)

Okay, the series is back.

Sorry for the shortness of this one. Just lost motivation. Upvote for longer ones :D

So let's start already.

Section 10: while loop.

Consider the code below

while:
  add eax, 1
  cmp eax, 5
  jl while
  ret

This is a while loop!

So at the end of the code we want we do a cmp and then do a conditional jump back, if that jump is not true we ret.

Section 11: Better print

Consider the code below:

section .text
  global _start
_start:
  mov si, msg
  call printf
printf:
  pusha
  loop:
    mov al, [si]
    cmp al, 0
    jne print_char
    popa
    ret
  print_char:
    mov ah, 0x0e
    int 0x10
    add si, 1
    jmp loop
section .data
  msg db, "Hello World", 0x0a, 0

That was a lot of code! Let's break it down.

ah, si, and al are more registers measuring 16, 8, and 8 bit in size, respectively.

pusha pushes everything to the stack.

popa pops everything from the stack.

int 0x10 is the BIOS interrupt to pint a single character stored in `al to the screen.

And what we are doing is looping until we find a 0, that is why we have the , 0 an=t the end of our string in the data section.

So you push the db to si and call printf.

Anyway.

Feedback, upvote.

Comments
hotnewtop
HahaYes (1914)

wuru do you have two alts? This is pretty suspicous tbh

programmeruser (576)

You don't really use real mode assembly unless you're writing a bootloader

xxpertHacker (861)

16, 8, and 8

You were intending to say that si and al are both 8 bit registers, right?

danielathome19 (0)

Syntax error on line 20 of section 11 - {msg db, "Hello ...} -- comma breaks the code after "db". Also, in attempting to run this program, on various different environments I get:
main.o: In function _start': main.asm:(.text+0x2): relocation truncated to fit: R_386_16 against.data'

In which case, my assumption was that it was attempting to truncate the message which would be 16 bits into si which is only 8: so I tried changing the registers from using si to esi, but I get the error:
timeout: the monitored command dumped core
sh: line 1: 19660 Segmentation fault timeout 10s demo

So far I haven't found a solution.

MesyetiIsTaken (55)

Randomly saw this in talk :) I'll find the previous and learn from it. Which version of Assembly does this use? I've been wanting to learn ARM Assembly

JustAWalrus (1187)

It uses x86 assembly :D intel syntax. @MesyetiIsTaken

TheForArkLD (776)

Yay, i need this REALLY

Highwayman (1443)

print_char:
mov ah, 0x0e
int 0x10
add si, 1
jmp str_loop

str_loop doesn’t seem to exist. Did you mean loop?

HahaYes (1914)

how did you just gain like 300 cycles

HahaYes (1914)

@Wuru you have gained over 300 cycles

HahaYes (1914)

@Wuru slightly sus of the account fallenangel13

firefish (1004)

@HahaYes If you notice it's not just @FallenAngel13 there is also @SarahHeisnoran

Jakman (473)

@HahaYes this is something i experience every day.

HahaYes (1914)

@Jakman lol, jakman nice logo

Jakman (473)

@HahaYes thx. Do you know what it is?

HahaYes (1914)

@Jakman I assume it is a greek symbol

Jakman (473)

@HahaYes it is the lambda symbol for the functional programming paradigm. Also it is the greek letter lambda.

Jakman (473)

@HahaYes what do you say that you and i make a file corruptor?

HahaYes (1914)

@Jakman ummm no. Also it is 10:30 pm at my end

Jakman (473)

@HahaYes thats cool. It's 9:30 pm here.

HahaYes (1914)

@Jakman but I mean, you can share it with me

Jakman (473)

@HahaYes alright cool. I'll make an engine real quick and when i get no errors and less than 50 warnings ill share it

Jakman (473)

@HahaYes alright. you can always ask questions as I have lots of time at my hands

eekboi (287)

@Jakman I know! Tis' scheme.