Mixing a notebook with linux printing

I did some tests in the call function of the c call, and I get what, in my opinion, weird behavior when using the ansi escape codes and calls the function c, which uses printf.

This is part of the assembly:

section .data               

    red db 27,"[31;1m",0
    redlen equ $ - red

    cyan db 27,"[36;1m",0
    cyanlen equ $ - cyan

    colorReset db 27,"[0m",0
    colorResetLen equ $ - colorReset

section .text

extern printLetter
extern letter

global main

main:
        mov  BYTE [letter], 'H'
        call ansiSetRed
        call printLetter
        mov  BYTE [letter], 'e'
        call ansiSetCyan
        call printLetter
        mov  BYTE [letter], 'l'
        call ansiReset
        call printLetter
        mov  BYTE [letter], 'l'
        call ansiSetRed
        call printLetter
        mov  BYTE [letter], 'o'
        call ansiSetCyan
        call printLetter
        mov  BYTE [letter], '!'
        call ansiReset
        call printLetter
        mov  BYTE [letter], 10
        call printLetter

        ret

ansiSetRed:
        mov rax, 1
        mov rdi, 1
        mov rsi, red
        mov rdx, redlen
        syscall
        ret

ansiSetCyan:
        mov rax, 1
        mov rdi, 1
        mov rsi, cyan
        mov rdx, cyanlen
        syscall
        ret

ansiReset:
        mov rax, 1
        mov rdi, 1
        mov rsi, colorReset
        mov rdx, colorResetLen
        syscall
        ret    

It looks a long time, but all I do is define some lines with ansi codes at the beginning, one for setting the foreground color to red, one for blue and from one to reset colors.

Then I have functions that print these ansi lines using syscall write.

The main function is to print "Hello!" alternating the color of each letter, first calling the assembly function, which prints the corresponding line ansi, and then calls the function extern c, which prints the character that is stored in the global variable.

c:

#include <stdio.h>

char letter;

void printLetter(void) {

    printf("%c", letter);

}

, "!". , ansi

enter image description here

c, :

#include <stdio.h>

char letter;

void printLetter(void) {

    printf("%c\n", letter);

}

, .

enter image description here

?

+4
1

, stdio ( - C) stdout, stdout . , , , , , . , ( ), , Hello , printLetter , stdout .

( , ):

  • edit ansiSetRed .., fwrite write. , .
  • setbuf(stdout, NULL), .
  • stderr stdout, stderr
  • fflush(stdout) printf stdout.
  • printLetter, write printf.
+8

Source: https://habr.com/ru/post/1663617/


All Articles