1

I have written a "method" is Nasm 64bit mode. I just want some advise on how I could have done it better as I have just started coding in Nasm. For example is there a way to use less registers, or are my naming conventions horrible(they probably are!), is there a better way to print negative values, etc.

bits 64

section .text
global _start

_start:
;debugging
 mov rbp, rsp

 push -666 ;number I want to print
 call _printVal ;calling the print "method"

 ;exit
 mov rax, 1
 mov rbx, 0
 int 80h


 _printVal:
 ;prolog
 push rbp
 mov rbp, rsp
 ;save registers
 push rbx
 push rdi
 push rsi

 ;actual code
 mov rax, [rbp + 16];get value user wants to print
 mov rbx, 10 ;we will be dividing by the to get the last digit
 xor rcx, rcx ; clear the rcx register as this will be out counter

 cqo; extend the flag
 cmp rdx, -1 ;check to see if negative
 jne _divisionLoop;if not negative jump
 ;print negative sign
 push 45;negative sign value
 mov rax, 1; sys_write
 mov rdi, 1; stdout
 mov rsi, rsp ; address to what to print
 mov rdx, 1 ; how many bytes is it
 syscall
 pop rax ;pop negative sign value from stack
 mov rax, [rbp + 16]; get user value again
 converting negative value to positive
 dec rax
 not rax
 xor rcx, rcx; clear rcx because of syscall again
 
 _divisionLoop:
    xor rdx, rdx
    div rbx ;divides number by 10 to move over last digit into rdx reg
    push rdx ;pushes the remainder onto the stack
    inc rcx   ; count for how many digits added to stack
    cmp rax, 0
 jnz _divisionLoop ;jump if the division did not result in a zero

 mov rax, 1; sys_write
 mov rdi, 1; stdout
 mov rdx, 1; 1 byte long


  _printToScreenVal:
    mov rsi, rsp; pop value from stack (most significant digit)
    push rcx; (save rcx)

    mov rcx, 48
    add [rsi], rcx; add 48 to make it into ASCII value

    syscall
    pop rcx; get rcx register back
    pop rbx; pop most siginificant digit
    dec rcx; decrement how many more digits i have to print
  jnz _printToScreenVal

  ;restore register
  pop rsi
  pop rdi
  pop rbx
  ;epilog
  pop rbp
  ret
Dagar
  • 143
  • 4
  • Requests for review of working code are meant to go to http://codereview.stackexchange.com instead of StackOverflow. – Nate Eldredge Sep 04 '20 at 03:50
  • 2
    If you're looking for code-review, post your question on http://codereview.stackexchange.com/. A few major things, though: x86-64 calling conventions pass args in registers, no need to waste instructions on stack args. [Don't use `int 0x80` in 64-bit code](https://stackoverflow.com/questions/46087730/what-happens-if-you-use-the-32-bit-int-0x80-linux-abi-in-64-bit-code). You don't need 2 loops to push and pop digits (padded to qwords), just store into a buffer: [How do I print an integer in Assembly Level Programming without printf from the c library?](https://stackoverflow.com/a/46301894) – Peter Cordes Sep 04 '20 at 03:51
  • Also, don't waste so many instructions pushing and popping. Just pick registers you can clobber. [What are callee and caller saved registers?](https://stackoverflow.com/a/56178078) – Peter Cordes Sep 04 '20 at 03:52

0 Answers0