Author Topic: Display string on console 64 bit using int 0x80  (Read 19011 times)

Offline TightCoderEx

  • Full Member
  • **
  • Posts: 103
Display string on console 64 bit using int 0x80
« on: May 08, 2012, 03:46:39 AM »
Display an ASCII string on console UBUNTU 12.04 using int 80H

Code: [Select]
; =========================================================================================================
; Display ASCII string on console (monitor)

; ENTRY: RDX = Pointer to null terminated string to be displayed.

; LEAVE: RAX = Bytes displayed not including null.
;       All other registers preseved.

; FLAGS: ZR = 1 If nothing was displayed.
; ---------------------------------------------------------------------------------------------------------

Console: push rbx
push rcx ; Preserve the registers we don't want to change
push rdx

; Determine where the end of null terminated string is

xor rax, rax
mov rbx, rax
inc ebx ; EBX = STDOUT
mov rcx, rax
dec rcx ; Should be sufficiently large
push rdi
mov rdi, rdx ; Set pointer
repnz scasb ; Find end of string
pop rdi ; Restore index to its original value

; We can do this a couple of ways, but I haven't experimented to see which way is best

neg ecx
dec ecx
dec ecx ; ECX = Actual size now
xchg rdx, rcx ; Swap so they are in proper position
mov al, WRITE ; Set sys_write number
int 0x80

mov rax, rdx ; Set return value
or rax, rax ; and set ZR

; Clean-up and go back

pop rdx
pop rcx ; Restore preserved registers
pop rbx

ret ; Return with string size in RAX and ZR=1 if string empty.

Null terminated strings are unconventional for nix's, but as using EDX in other platforms is a habit, I'm adopting the same philosophy here.  Not sure what goot the return value is going to be now, but I'm sure somewhere down the road it'll come in handy.
« Last Edit: May 08, 2012, 05:25:38 PM by TightCoderEx »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Display string on console
« Reply #1 on: May 08, 2012, 08:49:32 AM »
I'm confused. I thought 64-bit systems used "syscall", not int 0x80?

Best,
Frank


Offline TightCoderEx

  • Full Member
  • **
  • Posts: 103
Re: Display string on console
« Reply #2 on: May 08, 2012, 01:14:40 PM »
I thought 64-bit systems used "syscall", not int 0x80?

Of that I'm sure Frank, but as of yet have not been able to find a definitive text on how arguments are passed.  I'm assuming RDI, RSI, RDX, RCX & RBX and beyond that RDI as a pointer.  As I've just recently taken all that I had that was windows and thrown it out the window, these examples and others serve as tools to develop expertise in NASM, LD, GREP, NM and the wealth of other goodies available in a Nix system.

As programming is a hobby to me, the evolution is firstly, get it to work.  Post it so others with less experience might glean something from it and those with more can pick it apart.  Once there is some level of conformity, then tweak the algo to use the least amount of code and at the same time, try to cut cycles. To that end, I look forward to input from those on this board as it seems to be devoted to areas consistent of my interests.

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Display string on console
« Reply #3 on: May 08, 2012, 03:37:04 PM »
... but as of yet have not been able to find a definitive text on how arguments are passed.

The definitive text: http://www.x86-64.org

Offline TightCoderEx

  • Full Member
  • **
  • Posts: 103
Re: Display string on console
« Reply #4 on: May 08, 2012, 04:30:41 PM »
The definitive text: http://www.x86-64.org

Yes, I did stumble upon that site and I also found http://os1a.cs.columbia.edu/lxr/source/arch/x86/include/asm/unistd_64.h, so I think I'm on my way.