Author Topic: Display result  (Read 23757 times)

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Display result
« Reply #15 on: April 18, 2020, 07:26:39 PM »
Not tested, but maybe useful:
Code: [Select]
bits 64
default rel

section .rodata

hextable:
  db  "0123456789ABCDEF"

section .text

; From Windows API.
; You'll need to link with the corresponding static library for those (kernel32.lib?).
extern __imp_GetStdHandle
extern __imp_WriteConsoleA
extern __imp_ExitProcess

; Convert ECX to string pointed by RDX.
cvt2hex:
  mov r9d,ecx                        ; save ecx.
  mov ecx,28                         ; we'll start left shifting 28 bits.
.loop:
  mov   eax,r9d                      ; restore value to eax.

  shr   eax,cl                       ; isolate the corresponding nibble in LSB.
  and   eax,0x0f

  sub   ecx,4                        ; next time, left shift 4 bits less.

  movzx eax,byte [hextable+rax]      ; gets the hex digit...
  mov   [rdx],al                     ; ... and store in the buffer.

  add   rdx,1                        ; advance buffer position.

  test  ecx,ecx
  jns  .loop                         ; continue in the loop if ECX isn't negative.
  ret

; Print EDX chars from the buffer pointed by RCX.
Print:
  push  rsi
  push  rbx

  sub   rsp,8                        ; Allocate space for WriteConsoleA last argument.

  mov   esi,edx                      ; save EDX and RCX.
  mov   rbx,rcx                       

  mov   ecx,-11                      ; -11 is STD_OUTPUT_HANDLE constant (console api).
  call  qword [__imp_GetStdHandle]
  ; Here EAX contains the STD_OUTPUT_HANDLE console handle.

  xor   r9d,r9d                      ; num chars writen pointer is NULL.
  mov   r8d,esi                      ; num chars to write.
  mov   rdx,rbx                      ; pointer to char buffer.
  mov   qword [rsp],0                ; last argument is NULL.
  mov   rcx,rax                      ; console handle.
  call  qword [__imp_WriteConsoleA]

  add   rsp,8                        ; Deallocate space allocated on stack.

  pop   rbx
  pop   rsi
  ret

; Display ECX on console, in hexadecimal.
display:
  sub rsp,8               ; Allocate buffer of 8 chars.

  ; Convert ECX to string.
  mov r10,rsp
  mov rdx,rsp
  call  cvt2hex

  ; Print the string.
  mov edx,8
  mov rcx,r10
  call  Print

  add rsp,8               ; Deallocate the buffer.
  ret

  global  _start
_start:
  mov   ecx,0xDEADBEEF
  call  display

  ; Put the rest of your code here...

  xor   ecx,ecx
  jmp   qword [__imp_ExitProcess]

  ; Never gets here!

On Linux, with MinG64 installed, this should work (not tested):

Code: [Select]
$ nasm -fwin64 -o test.o test.asm
$ x86_64-w64-mingw32-ld -o test.exe test.o -e_start -L /usr/x86_64-w64-mingw32/lib -l:libkernel32.a

I leave to you to discover how to link this with microsoft linker...
« Last Edit: April 19, 2020, 11:10:21 AM by fredericopissarra »

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Display result
« Reply #16 on: April 18, 2020, 07:29:01 PM »
Win32 code is a little bit different (different calling convention).

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Display result
« Reply #17 on: April 18, 2020, 09:46:56 PM »
"r" = carriage return = 13 (0xd)
"n" = linefeed = 10 (0xa)

db "my string", 13, 10, 0

Best,
Frank

P.S. With "back quotes" Nasm may recognize
db /n/r
Curses! Why can't I print "back quotes" aka "back aposrtrophes'? Over the TAB key...

« Last Edit: April 18, 2020, 10:09:49 PM by Frank Kotler »

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Display result
« Reply #18 on: April 19, 2020, 11:14:03 AM »
P.S. With "back quotes" Nasm may recognize

db /n/r

Curses! Why can't I print "back quotes" aka "back aposrtrophes'? Over the TAB key...

Try to use code tags:
Code: [Select]
db `\r\n`
And, of course, using '\', instead of '/'

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Display result
« Reply #19 on: April 19, 2020, 08:12:34 PM »
Oops! Thank you Frederico!

Best,
Frank


Offline assistech

  • Jr. Member
  • *
  • Posts: 27
Re: Display result
« Reply #20 on: April 20, 2020, 10:36:41 AM »
Hi,


I would like to clear my screen with console API from this page: https://docs.microsoft.com/en-us/windows/console/clearing-the-screen


How can i integrate or call it in my NASM program ?

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Display result
« Reply #21 on: April 20, 2020, 11:02:23 AM »
I would like to clear my screen with console API from this page: https://docs.microsoft.com/en-us/windows/console/clearing-the-screen

See what the compiler does:
Code: [Select]
$ gcc -O2 -masm=intel -S -mtune=native test.cTake a look at test.s

Offline assistech

  • Jr. Member
  • *
  • Posts: 27
Re: Display result
« Reply #22 on: April 20, 2020, 11:13:13 PM »
Hi,


I am so sorry, i am suck at assembly, where can i find "test.s" ????



Can you add instruction on my program to show me how i can clear my screen with API windows ?







Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Display result
« Reply #23 on: April 21, 2020, 12:00:54 AM »
I am so sorry, i am suck at assembly, where can i find "test.s" ????
Write the code in C, compile with gcc using -S option and get the .s file (assembly listing)... Add -masm=intel option if you want Intel mnemonics.

I'll not do your work for you...

Offline assistech

  • Jr. Member
  • *
  • Posts: 27
Re: Display result
« Reply #24 on: April 21, 2020, 12:04:10 PM »
Great,


I used " gcc -S -masm=intel clearscreen.c" and i have obtain clearscreen.s file. Now i have "cls" in C converted to asm.

But my program don't work.... Why ???


 

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Display result
« Reply #25 on: April 21, 2020, 07:29:12 PM »
But my program don't work.... Why ???
Hummm... don't know... may because it is wrong?