LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   NASM, Jumping, and I'm new to this... (https://www.linuxquestions.org/questions/programming-9/nasm-jumping-and-im-new-to-this-147451/)

KneeLess 02-17-2004 11:31 PM

NASM, Jumping, and I'm new to this...
 
Hello. If anyone here knows assembly I'd sure like you to help me out. Here is my file:

Code:

section .text                                ;linker things, right?
        global _start


msg db  "Hello! It works!\n",0xa      ;my test things
len equ  $ - msg

_start:
        mov  edx,45                              ;move 45 into edx, and 46 into ecx
        mov  ecx,46
        test  edx,ecx                              ;test and jump to testFunc if edx is
        jle  testFunc                              ;less than ecx, which it is, if not, jump
        jmp  endThis                              ;to the end

testFunc:
        mov  edx,len                              ;Normal HelloWorld diddlies..
        mov  ecx,msg
        mov  ebx,1
        mov  eax,4
        int  0x80
        jmp  endThis                              ;Finish

endThis:
        mov  eax,1
        int  0x80

The logic is pretty easy to follow. The tutorial I was following used eax and ebx as registers for the test numbers, but I don't like useing those as..."variables". I know they're not variables, but they're for syscall things. I was uncomfortable. The problem is that when I run the program, output isn't printed to stdout.:confused: Also: are registers all letters? Like ecx, edx, eex, efx...etc.

Thanks

jinksys 02-18-2004 12:24 AM

First, You have TEST and CMP confused. TEST is used to check for the existance of certain bits in an operand, then sets FLAGS. CMP subtracts the two operands and sets FLAGS. For what you want to do, replace TEST with CMP and the example will work.

Second, { msg db "Hello! It works!\n",0xa } this would be correct had you been using a C
function like printf to output msg. But since you are using the write syscall, your \n will not be
interpreted and a \n will be outputted as well. For a newline you will have to use 0xA, the ASCII LineFeed character, for a newline (which was at the end of msg).

Third, the general purpose registers are:

32-bit:
eax
ebx
ecx
edx
esi
edi
ebp
esp
eflags, eip (not for general use)

16-bit:
ax
bx
cx
dx
si
di
bp
sp

16-bit segment registers:

CS, SS, DS, ES, FS, GS

8-bit(high):
ah
bh
ch
dh

8-bit(low):
al
bl
cl
dl

Hope I helped...

infamous41md 02-18-2004 12:46 AM

using '\n' with write is fine

jinksys 02-18-2004 01:29 AM

Quote:

Originally posted by infamous41md
using '\n' with write is fine
You need to read my post again,
I said that you cannot use \n with the
write SYSCALL. I also said \n would be
ok had KneeLess been using C to output
msg, but he is not. The C function write
can accept \n, but sycall #4 (write) does
not accept escape sequences.

jinksys 02-18-2004 02:18 AM

Incase you need proof, try running this code...
Code:

;        no_newline.s

segment .data
msg    db      "I do NOT have a newline.\n"
len    db      $-msg

segment .text

global  _start
_start:

mov    edx,len
mov    ecx,msg
mov    ebx,1
mov    eax,4
int    0x80

mov    eax,1
int    0x80

compile using:
Code:

~> nasm -f elf no_newline.s
~> ld no_newline.o -o no_newline



All times are GMT -5. The time now is 06:24 PM.