Skip to content

Instantly share code, notes, and snippets.

@Frityet
Created June 15, 2023 05:55
Show Gist options
  • Save Frityet/085d01dfcb7bcc2b91a663cfa202aa3e to your computer and use it in GitHub Desktop.
Save Frityet/085d01dfcb7bcc2b91a663cfa202aa3e to your computer and use it in GitHub Desktop.
; const char *, qword STRING.FROM_QWORD(char RDI buffer[RSI], qword RSI, signed qword RDX value)
export_function STRING.FROM_QWORD:
;qword R8 len = 0;
MOV R8, 0
;bool R9 negative = false;
MOV R9, FALSE
;if (value < 0)
CMP RDX, 0
JGE .0_l
;negative = true
MOV R9, TRUE
;value = -value
NEG RDX
;do
.0_l:
;R9 = value //save value, RDX is used for the remainder
;RAX = value / 10, RDX = value % 10
MOV RAX, RDX
MOV RDX, 0
MOV RCX, 10
DIV RCX
MOV R10, RAX
MOV RAX, RDX
MOV RDX, R9
;buffer[len] = RAX + '0'
MOV [RDI + R8], AL
ADD [RDI + R8], BYTE '0'
;len++
INC R8
;value = R10 (R10 is the result of the division)
MOV RDX, R10
;while (value != 0)
CMP RDX, 0
JNE .0_l
;if (negative)
CMP R9, TRUE
JNE .1_l
;buffer[len] = '-'
MOV [RDI + R8], BYTE '-'
;len++
INC R8
.1_l:
;buffer[len] = 0x0
MOV [RDI + R8], BYTE 0x0
; Reverse the string
;qword R10 i = 0;
MOV R10, 0
;for (i; i < len / 2; i++)
.2_l:
CMP R10, R8
JAE .3_l
;char R11 tmp = buffer[i];
MOV R11, [RDI + R10]
; char *R12 = buffer + len - i - 1;
MOV R12, RDI
ADD R12, R8
SUB R12, R10
DEC R12
;buffer[i] = buffer[len - i - 1];
MOV AL, [R12]
MOV [RDI + R10], AL
;buffer[len - i - 1] = tmp;
MOV [R12], R11
;i++
INC R10
JMP .2_l
.3_l:
;return buffer, len
MOV RAX, RDI
MOV RDX, R8
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment