-1

This is a piece of assembly language code that my professor has given me. Can you talk me through how it works? Especially the "int 80h" parts. I've looked online for the answers but it doesn't really make sense to me.

section .data string: db "Hello World", 0x0a 
len:     equ $ - string

section .text 
global main 
main:
       mov edx, len
       mov ecx, string
       mov ebx, 1
       mov eax, 4
       int 80h
       cmp eax, 0        
       je go

go:
       xor ebx, ebx
       mov eax, 1
       int 80h

I understand that edx, ecx etc are register locations, however, I don't get how it substitutes.

I know that mov is mnemonic for move and je is jump if equal to but the rest is a bit out of my comfort zone.

My prof has thrown us in at the deep end here and wants to know who can figure it out first so any help is always appreciated.

Rey
  • 3,663
  • 3
  • 32
  • 55
W_ Jonesey
  • 25
  • 1
  • 8
  • 1
    Your professor is getting paid to teach you. Maybe bring the issue up with him. Anyway, there are plenty of explanations for such a hello world but here is a quick version: `int 80h` is a service request to the operating system. It looks at what's in the registers to determine what to do. `eax` holds the function code aka. the system call number which in the first case is `4` for [write](http://man7.org/linux/man-pages/man2/write.2.html). The rest are the arguments. The second one is [exit](http://man7.org/linux/man-pages/man2/exit.2.html). The code thus does `write(1, string, 12); exit(0);`. – Jester Sep 28 '18 at 22:21
  • 2
    The `cmp`/`je` is totally useless here and is also wrong because `write` returns the number of bytes not a status code. – Jester Sep 28 '18 at 22:22
  • registers don't substitute... not sure how you think about them, but they are actually quite physical, `eax` being 32 bits (0 or 1 logical value) on the CPU chip (back in early x86 CPUs like 80386 there was only single physical `eax` inside CPU, modern CPU is more complex, but you can still think about it like that while programming). You can't substitute that for anything, it simple is there, and contains some pattern of 32 0/1 values, all the time, since the CPU is powered up. By `mov` you can change that bit pattern directly, like `mov eax, 4` will set those 32 cells to 00...00100. – Ped7g Sep 28 '18 at 22:37
  • 1
    Your prof has given you an assignment. Being *thrown in the deep end* is part of that assignment. If we help you figure it out first you've cheated, because you're not the one who figured it out first - we are. If you don't want to honestly make an effort to complete it yourself, don't bother doing it at all; let the person who really *figured it out first* get the credit for doing so. – Ken White Sep 29 '18 at 00:43
  • The 2nd half of my answer on [What parts of this HelloWorld assembly code are essential if I were to write the program in assembly?](https://stackoverflow.com/a/39551489) has the same i386 Linux code in NASM, with only different variable names. It has a fairly complete explanation of what's going on when making a system call. (Except mine doesn't have the useless branch. `sys_write` returns a byte count or a negative `-errno`. And if it was, the branch target is the next instruction anyway, so taken and not-taken go to the same place.). – Peter Cordes Sep 29 '18 at 03:52

1 Answers1

0

Firstly you should look x86 system_call. Then you can understand main func. "Hello world" in the first line is set to string value.The second line calculated the length of the variable and then system calls are used to print the message on the screen. If the operation fails, the program jump to "go" and call system_exit with "mov eax ,1". Finally "int 80h" means call kernel

  • Your explanation of the `cmp/je` isn't quite right. The branch target is the next instruction, so whether it's taken or not, the next instruction to execute is always the one after `go:`. And `cmp eax,0` isn't how you check for `sys_write` failure: Linux system call error codes are `-1..-4095`. `sys_write` can apparently return 0 if nothing was written, but I think that's only possible if you pass count=0. http://man7.org/linux/man-pages/man2/write.2.html#RETURN_VALUE – Peter Cordes Sep 29 '18 at 03:58