Recent Posts

Pages: 1 ... 4 5 [6] 7 8 ... 10
51
Programming with NASM / Re: Printing numbers with stack as variable
« Last post by fredericopissarra on December 29, 2024, 12:52:05 PM »
For your study:
Code: [Select]
  bits  64
  default rel

  section .rodata

nl:
  db  `\n`

  section .text

  global _start

  align 4
_start:
  sub   rsp,8               ; Align RSP to DQWORD (SysV ABI)

  mov   rdi,12345678        ; # to print.
  call  printUint64Decimal

  ; Print a newline
  mov   eax,1
  mov   edi,eax
  mov   edx,eax
  lea   rsi,[nl]
  syscall

  sub   rsp,8               ; restore RSP

  ; Exit program
  mov   eax,60
  xor   edi,edi
  syscall                   ; This syscall never returns.

; Entry RDI = #
  align 4
printUint64Decimal:
  ; Allocate 24 bytes, realigning RSP to DQWORD (SysV ABI).
  ; We just need 22 bytes in the buffer allocated on the stack.
  sub rsp, 24

  mov r9, rsp
  mov rsi, rsp

  mov r8, 0xcccccccccccccccd      ; 1/10, scaled (0b0.00011001100... rounded and shifted left by 67).

  align 4
.loop:
  mov rax, rdi
  dec rsi

  ; Multiply by scaled 1/10, instead of dividing by 10
  ; This is faster.
  mul r8
  mov rax, rdi
  shr rdx, 3                      ; RDX = quotient

  lea rcx, [rdx+rdx*4]            ; RCX = RDX*10
  add rcx, rcx
  sub rax, rcx                    ; RAX = Dividend - RCX (remainder)
  ; RAX = remainder, RDI = quotient

  ; Store remainder converted to ASCII.
  add al, '0'
  mov [rsi], al

  mov rax, rdi
  mov rdi, rdx

  ; Stay in loop if quotient > 9.
  cmp rax, 9
  ja  .loop

  ; Print the buffer, calculating the size of the string.
  mov eax, 1
  mov rdx, r9
  sub rdx, rsi
  mov edi, eax
  syscall

  add rsp, 24
  ret
52
Programming with NASM / Printing numbers with stack as variable
« Last post by andyz74 on December 29, 2024, 11:34:50 AM »
Hello all, I hope you enjoy winter-holidays! :-)

But now to my problem :
I use Linux 64 bit, and my wish is to print numbers on the screen just by syscall, no extern funtions and no extra variables for storing numbers.  Therefore, I use the common way, push every number to stack to get it later and print.
I know, the syscall to write is with rax=1, rdi=1 to use my screen, rsi as pointer to the adress where my string/number is, and rdx for length of string (in my case 1).
As you can see, I use the stackadress in rsi to get my number, and it works with 8 added or 16 added, but not with the common rsp-adress.
Has anybody of you an idea, what is going wrong ? 

Greetz, and thanks in advance, Andy



Code: [Select]
[bits 64]

SECTION .data
    n: dd 1234

global _start

SECTION .text

    _start:

  mov rax, [n]
  xor rcx, rcx       

.loop:
  mov rbx, 10
  xor rdx, rdx
  div rbx            ; Divide RAX by 10
  add rdx, 30h
  push rdx   ; pop Restzahl auf Stack
  inc rcx
  test rax, rax
  jnz .loop

.loop2:
  mov rax, 1         ; Write system call
  mov rdi, 1         ; STDOUT
  xor rsi, rsi
  mov r10, rsp
  add r10, 0 ; change offset to be read in stack 0, 8 or 16
  mov rsi, r10
  pop r12
  mov rdx, 1 ; ein Zeichen schreiben.
  push rcx
  syscall
  pop rcx
  dec rcx
  test rcx, rcx
  jnz .loop2


   mov rax, 1
   mov rbx, 0
   int 0x80


53
Using NASM / Re: Conditional Assembly question
« Last post by ReturnInfinity on December 05, 2024, 02:55:12 AM »
That works! I just had to change all instances of %ifenv to %ifdef in my code.

Thank you!
54
Other Discussion / Awesome feeling
« Last post by tysonprogrammer on November 30, 2024, 11:27:45 PM »
I was playing around with Nasm on Fedora Linux 41 and figured out how to make system calls instead of linking to the C library. Seems silly to link to the C library in assembly, why not just write it in C.

Anyway just wanted to document my success.

Tyson
55
Using NASM / Re: Conditional Assembly question
« Last post by fredericopissarra on November 28, 2024, 11:46:55 AM »
Try this:
Code: [Select]
$ nasm -DBIOS=1 pure64.asm -o ../bin/pure64-bios.sys -l ../bin/pure64-bios-debug.txt
$ nasm -DUEFI=1 pure64.asm -o ../bin/pure64-uefi.sys -l ../bin/pure64-uefi-debug.txt
56
Using NASM / Conditional Assembly question
« Last post by ReturnInfinity on November 26, 2024, 01:10:16 AM »
Hello everyone,

I'm starting to make use of the Conditional Assembly to check for environmental variables at build time. Currently I have some boot code that contains functions for BIOS and UEFI systems and am building two separate binaries with the same source:

Code: [Select]
export BIOS=1
nasm pure64.asm -o ../bin/pure64-bios.sys -l ../bin/pure64-bios-debug.txt
unset BIOS
export UEFI=1
nasm pure64.asm -o ../bin/pure64-uefi.sys -l ../bin/pure64-uefi-debug.txt
unset UEFI

This works but is it the expected method for using environmental variables? Not sure if there is a cleaner way to accomplish this.

Thanks,
-Ian
57
Programming with NASM / Question about USB programming
« Last post by ben321 on November 04, 2024, 09:02:28 AM »
I wanted ro know, how one does programming to access USB devices (such as webcams) in assembly. I know that some hardware is memory mapped (such as VGA video mapped to 0xA0000), but others are accessed by calling interupts (or certain hardware actions actually trigger interupts). While others have to be accessed directly with the IN and OUT instructions (which actually happens behind the scenes with INT based functions for accessing certain hardware), if no INT function is available. I'm curious how USB devices are accessed. I know NASM can compile 16bit code for DOS, so I'd be interested in trying to write code that accesses USB devices from DOS.
58
Using NASM / Re: windows x86 64bit help
« Last post by hockey97 on August 31, 2024, 04:47:54 PM »
Nevermind... I got it working. It was looking for the files but couldn't access them due to permission issues. I fixed it and it now works.
59
Using NASM / Re: windows x86 64bit help
« Last post by hockey97 on August 30, 2024, 10:24:15 PM »
you cannot just convert the 32-bit source to 64-bit by just switching the assembler's command-line.

just notice the difference between the Hello World samples in 32-bit assembly and a 64-bit assembly.

32-bit are more into pushes when feeding data into an API that you wanna run..
64-bit is somehow similar to DOS..

I know that. Why would you say such a thing when I am not asking to force a 32bit program to work like a 64bit program.

I wrote a 64 bit progfram in ASM for windows but it needs to use the abi 64bit version.

I am using RAX and EAX.  The issue isn't the code.  The code works for 64 bit windows machines.

I am asking how do you link the files for windows to use a 64 bit program. You need to use the windows api to make a windows app.


60
Using NASM / Re: windows x86 64bit help
« Last post by alCoPaUL on August 30, 2024, 09:13:10 AM »
you cannot just convert the 32-bit source to 64-bit by just switching the assembler's command-line.

just notice the difference between the Hello World samples in 32-bit assembly and a 64-bit assembly.

32-bit are more into pushes when feeding data into an API that you wanna run..
64-bit is somehow similar to DOS..
Pages: 1 ... 4 5 [6] 7 8 ... 10