Jump to content

Good tutorial

- - - - -

  • Please log in to reply
15 replies to this topic

#1
Alexio

Alexio

    Newbie

  • Members
  • Pip
  • 4 posts
Please advise me some good tutorial on Assembler.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
We have a fine index of assembly tutorials here at our site:
Assembly Tutorials

You may wish to start with the "Intro to Intel Assembly Language" one.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
Hey guys, can you please suggest me a good eBook? I found some but I don't know which one to use. I correctly have "Programming Ground Up", "The Art Of Assembly Language", and "PC Assembly". Can you advice a good eBook?(Let me remind you that I'm using Gentoo and NASM).
Thanks

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
AOAL is available as a PDF, if that's what you mean.
sudo rm -rf /

#5
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
Yeah I already have it. I asked which one of the mentioned is best or if there's any better one you may know, but I'm staying with AOAL.
Thanks anyway :)

-EDIT-
Holy netbooks, this thing has 1361 pages of pure Assembly! This is gonna take some time to learn, considering that I'm not even done with C. :w00t:

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Yeah, I know. Good times. :D
sudo rm -rf /

#7
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
God, I'm really starting to like this Assembly thingy, even before I finished the introduction. It says that ASM is light, and I was like "Really? How light?", so I tested it. Here's Hello World in C -

Quote

#include <stdio.h>

int main()
{
puts("Hello World!");
}
Size - 1.50 KB (1,536 bytes).(Tiny C Compiler)
Just cause I was curious, I compiled it with Borland TurboC 3.0.
6.93 KB (7,106 bytes). Geez.

Here's a Hello World for NASM -

Quote

section .text
org 0x100
mov ah, 0x9
mov dx, hello
int 0x21

mov ax, 0x4c00
int 0x21

section .data
hello: db 'Hello, world!', 13, 10, '$'
Copied from Wikipedia to test my compiler.
28 Bytes! Unbelievable! It weights less than it's source!

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Yes, but that only works in real mode. Try running that from inside Windows or Linux.
sudo rm -rf /

#9
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
You mean pasting it in notepad and saving as foo.exe?
Yeah, I tried it. It gave me an error, something about illegal actions.

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
You can't just write code in any language you choose, save it with a .EXE extension and expect it to execute. You have to compile it first into a format the operating system understands. For example, under Linux:

nasm -f elf -Wall -Werror -o mycode.o mycode.obj


This gets you an object file (in ELF format) that you still have to link to the runtime system:

gcc -o myprogram mycode.o


Now you can run myprogram. I can tell you right now, though, that your program as written won't link. The C runtime system expects a function called main to be defined, so you're going to have to rewrite your code like this:

; let the C runtime system see the main function

global main


section .text

main:

    mov     ah, 0x9

    mov     edx, hello

    int     21h


    mov     ax, 0x4c00

    int     21h


section .data

    hello:  db "Hello, World!"


Perform the above steps for compiling and linking, run it in the terminal, and expect a segfault. You cant use BIOS interrupts in protected mode on any operating system. Plus, you don't have a ret instruction to return to the C runtime system so the operating system is going to try and continue executing whatever comes in memory after your code, which is unpredictable. This, on the other hand, makes the proper system calls and will work on Linux:


global main


section .text

main:

    ; have to save EBX, EBP, ESI, EDI by convention

    push    ebx


    ; make a call to write().

    mov     eax, 4          ; function 4

    mov     ebx, 1          ; handle to write to (stdout)

    mov     ecx, hello      ; buffer to write from

    mov     edx, 14         ; number of bytes to write

    int     0x80            ; syscall interrupt

    

    pop     ebx

    ret

    

section .data

    hello:  db "Hello, World!", 0x0a


Compilation/Linking:

nasm -felf -O3 -Wall -Werror -o test.o test.asm

gcc -s -O3 -o test test.o


That comes out to 5508 bytes on my system (32-bit Ubuntu), though I know I screwed up the optimization.

EDIT: I know how I can strip out the C runtime library, which gets it down to 480 bytes, but it segfaults at the end. You need to make a few changes to the code, but I'm not going to post it until I get rid of the segfault.
sudo rm -rf /

#11
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
I was sure it won't run just cause I saved it as an exe, I was just curious what would happen.
Ehh... You just confused me. The program compiled/linked fine, does Assembly compile anyway?
I saved it as "Hello World.asm" under Windows XP, and used nasm.exe to compile it - "nasm "Hello World.asm" -o hi.exe". Of course, Linux's code is different.
Well, I'll try it soon on Debian as that's the only one I have here(other than Windows 7).

+rep, you deserve it.

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
nasm just compiles, you need something else to link it. I don't know what it is on Windows, as I've never bothered writing ASM for it. (And thanks for the rep.)
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users