Author Topic: Debian Newbie has FIVE questions  (Read 17012 times)

nobody

  • Guest
Debian Newbie has FIVE questions
« on: March 24, 2006, 06:38:16 PM »
Hi all.

A debian user here. I am new to both NASM and assembly. So please bear with me here.

Wrote a program to try to capture 2 user inputs, add them up, and output to screen.

MOV DX, 0060H   ;prepare keyboard
IN EAX, DX      ;get user input #1
MOV EBX, EAX    ;save input #1 to EBX
MOV DX, 0060H   ;prepare keyboard
IN EAX, DX      ;get user input #2
ADD EAX, EBX    ;add input #1 to #2
;OUT xxxxxx      ;output result to monitor screen
RET            

ok...i used >cat /proc/ioports to check out the IO address of my keyboard. Its listed as 0060-006f : keyboard. Hence I used it in my code, by moving the address to DX, then invoking IN.

QUESTION 1: am i doing the right thing?
QUESTION 2: How about outputting to screen? I dunno the IOPORT of my monitor. How can I check?
QUESTION 3: Are there any other ways to port to screen?

I saved my program as add.asm. Then I tried to compile by typing:
>nasm -f elf add.asm

It generated a file add.o

QUESTION 4: What do I do with this file? Do I try to link it? May I know what command to use?

QUESTION 5: What file do I finally run it?

Mighty thanks. I just need to get started. Will appreciate any help.

*Debiabie*

nobody

  • Guest
Re: Debian Newbie has FIVE questions
« Reply #1 on: March 25, 2006, 01:02:02 AM »
Well... "add eax, ebx" is right... Otherwise... no, you're not "doin' it right". :) You'd have to get "permission" to access the keyboard ports, and then... An interrupt is generated when a key is pressed - or released - and has to be "handled". Some keys result in more than one byte at port 60h, and in any case, the value read there would have to be translated (via lookup table) to get an ascii character value out of it. Really not the way you want to do it.

What you want to do is read/write from/to stdin/stdout. An extremely simple example that doesn't even prompt for input - just waits for input and spits it back...

global _start ; entrypoint - ld wants to know this
BUFSIZ equ 100 ; ought to be big enough

section .bss
    buffer resb BUFSIZ

section .text
_start:
    mov eax, 3 ; number for "sys_read"
    mov ebx, 0 ; stdin
    mov ecx, buffer ; address of buffer, not "[contents]"
    mov edx, BUFSIZ ; maximum to read
    int 80h  ; call kernel

mov edx, eax  ; sys_read returns length in eax
    mov ecx, buffer
    mov ebx, 1 ; stdout
    mov eax, 4 ; "sys_write" - "__NR_write", actually
    int 80h

mov ebx, 0 ; exit code
    mov eax, 1 ; sys_exit
    int 80h

Assemble with "nasm -f elf myfile.asm". Link with "ld -o myfile myfile.o". Run as "./myfile".

You can't exit with "ret", 'cause there's no return address on the stack - just "argc", a zero-terminated array of pointers to zero-terminated strings (command line args), followed by a zero-terminated array of pointers to zero-terminated strings (the environment variables). If you start your program with "main" and link it with gcc (the C startup code), then "main" is called, and you can "ret".

To actually add two numbers, we still have the job of converting the ascii string "123" into the number 123 (twice), adding them ("add eax, ebx"), the converting the resulting number in eax *back* to an ascii string so it can be displayed (this is the part where we can represent the number as binary, decimal, hex, octal, or whatever). This part isn't specific to Linux, and you should be able to find examples...

You probably want to look at http://www.linuxassembly.org - mirrored here es http://asm.sf.net - start with the Linuxassembly Howto, I guess. For more "general" asm information - http://www.drpaulcarter.com/pcasm perhaps...

global _start

section .data
    msg db "Welcome to Linux Assembly!, 10
    msg_len equ $ - msg

section .text
_start:

mov eax, 4
   mov ebx, 1
   mov ecx, msg
   mov edx, msg_len
   int 80h

mov eax, 1
   mov ebx, 0
   int 80h

Warning: The lovely forum software is prone to "optimize" posts by removing spaces before periods etc. There's supposed to be a space between "section" and ".text", for example... in case you're not seeing it. Nasm does care.

Best,
Frank

nobody

  • Guest
Re: Debian Newbie has FIVE questions
« Reply #2 on: March 25, 2006, 06:10:12 PM »
Thanks a lot, Frank!

Got my first NASM assembly language compiled nicely! :)

I'll try to find my feet around from now on. Definitely checking out the websites and maybe this website too eh?

http://www.geocities.com/SiliconValley/Heights/1295/clueless.html

cheers,
Denewbian