Table of Contents

Wiki Page for the COMPUTER ORG class. Please Enhance.

For those unfamiliar, here is a page on wiki editing syntax that can be used here.

Assembling/Compiling "first" code

There's been some trouble reported over the assembly and compilation of the first code sample, one that uses the files asm_io.asm, first.asm, and driver.c … depending on how you go about it, it may work, or not work :) Following are some instructions you can follow so that it will in fact work:

Obtain Files

lab46:~/asm$ cp /var/public/asm/bookcode/asm_io.asm .
lab46:~/asm$ cp /var/public/asm/bookcode/asm_io.inc .
lab46:~/asm$ cp /var/public/asm/bookcode/first.asm .
lab46:~/asm$ cp /var/public/asm/bookcode/cdecl.h .
lab46:~/asm$ cp /var/public/asm/bookcode/driver.c .
lab46:~/asm$ ls
asm_io.asm  asm_io.inc  cdecl.h  driver.c  first.asm
lab46:~/asm$ 

asm_io.asm contains the actual assembly code for the various functionality being used in other programs (like first.asm).

asm_io.inc is similar in nature to a header file is in C and C++, identifying important symbols.

first.asm is the actual program.

cdecl.h is a header file which helps to set the appropriate calling conventions to allow interaction between the C and assembly files.

driver.c is a C program that calls the assembly program (not really necessary, but a great example showing you can mix C/C++ and assembly).

Assemble the assembly files

lab46:~/asm$ nasm -f elf -d ELF_TYPE asm_io.asm
lab46:~/asm$ nasm -f elf -d ELF_TYPE first.asm
lab46:~/asm$ ls
asm_io.asm  asm_io.inc  asm_io.o  cdecl.h  driver.c  first.asm  first.o
lab46:~/asm$ 

Note that when you assemble a file, it creates a .o file, which is the corresponding object code. This is in the native binary code for the machine, but doesn't have the necessary code to actually run. We'd need to link it against the system libraries (using ld, or for the ease of this example, allow gcc to do it while we compile the C portion).

lab46:~/asm$ gcc -o first driver.c asm_io.o first.o
lab46:~/asm$ ls
asm_io.asm  asm_io.inc  asm_io.o  cdecl.h  driver.c  first  first.asm  first.o
lab46:~/asm$ 

You can now run “first”, which is the executable as formed from the object files of all the assembled and compiled files (gcc sort of abstracts this away from us for the C files, as it does compiling to object and linking in that one step).

TODO

I announced this in class, but in case anyone missed it or didn't do it at the time.

At your next convenience, I'd like everyone to log into Lab46, and from the base of their home directory, run the following command (example command-line included for emphasis):

lab46:~$ ln -s /usr/local/etc/data/$USER data

This will bring you closer to the grade not-z, and allow you to bask in its infinite love, via your own personalized “data” symlink in your home directory.

Those who have already created their journals will notice the grade not-z has already paid you a visit.

Intel Assembler 80×86 CodeTable http://www.jegerlehner.com/intel/IntelCodeTable.pdf
Hello World FAQ (some good notes in here): http://home.comcast.net/~fbkotler/clueless.html
Some sample nasm programs (with equivalent C code): http://www.csee.umbc.edu/portal/help/nasm/sample.shtml#Top
gdb (some commands and such for nasm): http://www.cs.umbc.edu/portal/help/nasm/nasm.shtml

One more good resource can be found here: http://www.youtube.com/watch?v=4VNgd3WM95E The above is a series of 12 or so videos that take you step by step through the basics of NASM (without a C interface!).

nasm x86 Assembly Quick Reference (“Cheat Sheet”): http://www.cs.uaf.edu/2006/fall/cs301/support/x86/

EAX register sys call

cd: /usr/include/asm-x86_64
edit or cat | less to view: unistd.h
Great link for register syscall

Assembly Primes Example

http://lab46.corning-cc.edu/user/lleber/start#prime_number_assembly_solution_a

Hello World Example

  1 ; Basic input/output functionality in assembly
  2
  3 %define SYS_exit    1
  4 %define SYS_write   4
  5
  6 section .text
  7
  8 global _start
  9
 10 _start:
 11     nop
 12
 13 write_hello:
 14     mov edx, hello_len
 15     mov ecx, hello
 16
 17 .loop:
 18     mov eax, SYS_write
 19     mov ebx, 1
 20     int 80h
 21
 22     cmp eax, -4096
 23     ja error
 24
 25     add ecx, eax
 26     sub edx, eax
 27     jnz .loop
 28
 29 ok:
 30     mov eax, SYS_exit
 31     xor ebx, ebx
 32     int 80h
 33     hlt
 34
 35 error:
 36     mov eax, SYS_exit
 37     mov ebx, 1
 38     int 80h
 39     hlt
 40
 41     section .rodata
 42 hello: db "Hello, World!", 10
 43 hello_len equ $-hello

WTF Does that mean?

DB means “Define Byte and so it does” ex.

prompt1 db    "Enter a number: ", 0

this defines a variable prompt1 as the string “Enter a number: ”

SECTION / SEGMENT is comparable to an “#include” in C or c++. It is used to read a library.

segment .text

EAX, EBX, ECX, ect… all these are registers, think of them as locations… that have commands…

,0 or ,0x…, or,[anything] this is how to end a variable it puts a character at the end, it is all in ascii code, 0x means using hex, so 0x0a would be the new line character. just a 0 would make it octal, so 012 would be new line character.

Getting Input from Users

mov eax, 3
mov ebx, 1
mov ecx, input ;input is the variable the input is to be stored to
mov edx, 4     ;size of the input in bytes
int 0x80

Do Not Argue With my Arguments!

The defaults assumed by NASM if you do not specify the qualifiers shown below:

          section .text progbits alloc   exec nowrite align=16 
          section .data progbits alloc noexec   write align=4 
          section .bss    nobits alloc noexec   write align=4 
          section other progbits alloc noexec nowrite align=1

99 Bottles of Beer Example

This is an example of an assembly program to do the '99 Bottles of Beer' song.

http://99-bottles-of-beer.net/language-assembler-%28intel-x86%29-1144.html

Input Validator [Work In Progress]

Not done completely yet, makes use of the SYS_IOCTL invocation

http://lab46.corning-cc.edu/user/lleber/start#input_validator