Howdy Friends;
I have just started learning assembly, and am currently working with NASM, in Linux. I am not creating anything close to advanced, yet because I would like to know if I am on the right path with what I am learning. The code that is posted below is simply a hello world with two lines of output (but it doesn't work the way I intended). The output that I was hoping for was:

Howdy Folks!
Another line!

However; I simlpy get:

Another line!

Here is my code:

SECTION .text
            global start
START:
            mov eax, 4          ;sys_write
            mov ebx, 1          ;output to screen
            mov ecx, string ;creates address for my string variable
            mov ecx, string2    ;creates address for another string
            mov edx, length ;creates address for my length variable
            mov edx, length2    ;creates address for another length
            int 80h             ;call the kernel
            mov eax, 1          ;sys_exit
            mov ebx, 0          ;no error exit
            int 80h             ;call the kernel
SECTION .data
string: db 'Howdy Folks!', 0Ah  ;output string
length: equ 12                      ;length of string
string2: db 'Another line!', 0Ah    ;output another string
length2: equ 13                 ;length of string2

Where did I go wrong?
Did I accidentally overwrite string ('Howdy Folks') with the value of string2 ('Another Line!')?
How can I go about fixing this?
Also, are my comments appropriate with how the line actually functions?

For instance;

int 80h             ;call the kernel

Does this line really call the kernel?
If so what is it actually doing there?

Thanks for any help.

Recommended Answers

All 2 Replies

Basically, you would need to call the interrupt twice, once for each string you are printing.

SECTION .text
            global _start
_start:
            mov eax, 4          ;sys_write
            mov ebx, 1          ;output to screen
            mov ecx, string     ;creates address for my string variable
            mov edx, length     ;creates address for my length variable
            int 80h             ;call the kernel
            mov eax, 4          ;sys_write
            mov ebx, 1          ;output to screen           
            mov ecx, string2    ;creates address for another string
            mov edx, length2    ;creates address for another length
            int 80h             ;call the kernel
            mov eax, 1          ;sys_exit
            mov ebx, 0          ;no error exit
            int 80h             ;call the kernel

SECTION .data
string: db 'Howdy Folks!', 0Ah  ;output string
length: equ 13                      ;length of string
string2: db 'Another line!', 0Ah    ;output another string
length2: equ 14                 ;length of string2

Note that the code as given had a number of problems with it; for example, the entry point should have been _start, not START (otherwise the standard Linux linker, ld, won't find it), and the lengths of the strings should have included the newlines.

As for what int 80h does, it signals a software interrupt, a low-level mechanism used to send requests to the kernel. When an interrupt occurs, whether from the hardware or from a program, the current program is halted and the system goes into a kernel routine mapped out to handle the interrupt. The interrupt mapped for hex 80 (decimal 128) happend to be mapped to a set of operating system services which get selected from a dispatch table based on the value in the EAX register. This list of the INT 80h system calls may prove useful for you.

Mind you, even in assembly language, it is probably more common to use the standard Linux library than to call the kernel directly.

In pseudo code terms, what you're doing is:

EAX = 4
EBX = 1
ECX = Location of string 1
ECX = Location of string 2
EDX = Length of string 1
EDX = Length of string 2
SYSTEM CALL

So you're setting ECX and EDX to certain values, then overwriting them with other values. This means that when int 0x80 is invoked, ECX points to the location of string2 and EDX points to the length of string2. You need a separate system call for each individual string you'll be printing.

Please also note that you've defined the length of string1 as being 12. This means that when you print it, it'll print the characters "Howdy Folks!" but not the 0Ah which follows. 0Ah is the charcter to print a new line on Linux/UNIX. You've also made the same mistake with string2.

Also, are my comments appropriate with how the line actually functions?

Your comments look pretty much fine, with the exception of the following line

mov ecx, string ;creates address for my string variable

It doesn't create the address as such, it just moves the address that string represents into ECX. When you assemble your code, the assembler works out the memory address that the labels in your program (start, string, string2, etc.) will end up at when it runs. It then replaces any occurence of this label with that address. So with the above line of code, it moves the address of your string into ECX.

Does this line really call the kernel?
If so what is it actually doing there?

The Linux kernel will have set-up entries in something called the IVT - Interrupt Vector Table. It's just a fancy word for "Table of what code to execute when a specific interrupt is called". When you call int 0x80 some kernel code is executed which decides what on earth to do, based upon the values in various registers.

Take the example of:

mov eax, 1
mov ebx, 0
int 80h

You make EAX equal 1 and EBX equal 0, then call Interrupt 0x80 or Interrupt 80h. This takes control away from your program and passes it to the Kernel. It sees that EAX=1 and knows it has to quit your program with the error code that you specified in EBX.

So an int 0x80 does what's commonly referred to as a System Call - that is it gets the Operating System to perform a certain operation.

commented: Thank You, I appreciate your time. You and Schol-R-LEA definitely helped me out. This shall be the first of many questions on my Assembly programming journey. Thank You +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.