Answer to Question #259396 in C for Anuj Yadav

Question #259396

Two code snippets are being presented to you.


1. mov rax, 0x1234567812345678


xor ax, 0x11


mov rdi, ax


call printf


xor rax, 0x11


mov rdi, rax


call printf






2. int x=-2;


unsigned int y = -33;


int z;


z = x + y;


printf(‘‘%u %u %u’’, x,y,z);


printf(‘‘%d %d %d’’, x,y,z);



In both the parts (1) and (2), explain what the two printf function calls


result in. Explain the reasons for any differences in the two cases.


1
Expert's answer
2021-11-01T01:58:52-0400

call printf function is a function that gets output from plain without using cout. Simply pass the format string in rdi as the first argument, any format specifier arguments in rsi, then rdx, and so on to call printf from assembly language. If you don't zero the al register (the low 8 bits of eax/rax), printf will think you're giving it vector registers and crash.

For example:

mov rdi,formatStr ; first argument: format string

mov rsi,5 ; second argument (for format string below): integer to print

mov al,0 ; magic for varargs (0==no magic, to prevent a crash!)

extern printf

call printf

ret

formatStr:

db `The int is %d\n`,0

The printf() function sends a formatted string to the standard output.

For Example:

int i;

for (i=1;i<=20;i++)

printf(" now i=%d\n",i);

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS