0% found this document useful (0 votes)
130 views

Lec - 6 Codes

This document contains three code examples that demonstrate how to call assembly code from C: 1) The first example shows how to print a string from assembly using the puts function. It defines a main label, declares puts as external, and calls puts with the message string in rdi before returning. 2) The second example prints numbers from 1 to 90 using printf. It saves registers before calling printf, passes the format string and number, then restores registers after the call. 3) The third example finds the maximum of three integers by defining a function in assembly that returns the max in rax. It is called from a C program that passes different combinations of values and prints the results.

Uploaded by

Iqbal Uddin Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

Lec - 6 Codes

This document contains three code examples that demonstrate how to call assembly code from C: 1) The first example shows how to print a string from assembly using the puts function. It defines a main label, declares puts as external, and calls puts with the message string in rdi before returning. 2) The second example prints numbers from 1 to 90 using printf. It saves registers before calling printf, passes the format string and number, then restores registers after the call. 3) The third example finds the maximum of three integers by defining a function in assembly that returns the max in rax. It is called from a C program that passes different combinations of values and prints the results.

Uploaded by

Iqbal Uddin Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

; Using C command in assembly

; File named CnA.asm


global main
extern puts
section .text
main: ; This is called by the C library start
up code
mov rdi, message ; First integer (or pointer) argument in
rdi
call puts ; puts(message)
ret ; Return from main back into C library w
rapper
message:
db "Hamdard University", 0 ; Note strings must be terminated
with 0 in C
- How to compile
$ pico CnA.asm
$ nasm -felf64 CnA.asm
$ gcc -o CnA CnA.o
$ ./printf
; printf.asm
global main
extern printf
section .text
main:
push rbx ; we have to save this since we use it
mov ecx, 90 ; ecx will countdown to 0
xor rax, rax ; rax will hold the current number
xor rbx, rbx ; rbx will hold the next number
inc rbx ; rbx is originally 1
print:
; We need to call printf, but we are using rax, rbx, and rcx. printf
; may destroy rax and rcx so we will save these before the call and
; restore them afterwards.
push rax ; caller-save register
push rcx ; caller-save register
mov rdi, format ; set 1st parameter (format)
mov rsi, rax ; set 2nd parameter (current_number)
xor rax, rax ; because printf is varargs
; Stack is already aligned because we pushed three 8 byte registers
call printf ; printf(format, current_number)
pop rcx ; restore caller-save register
pop rax ; restore caller-save register
mov rdx, rax ; save the current number
mov rax, rbx ; next number is now current
add rbx, rdx ; get the new next number
dec ecx ; count down
jnz print ; if not done counting, do some more
pop rbx ; restore rbx before returning
ret
format:
db "%20ld", 10, 0

- How to compile
$ pico printf.asm
$ nasm -felf64 printf.asm
$ gcc -o printf printf.o
$ ./printf

; Calling Assembly in C
; File name Asm.asm
; The Assembly Code
global Asm
section .text
Asm:
mov rax, rdi ; result (rax) initially holds x
cmp rax, rsi ; is x less than y?
cmovl rax, rsi ; if so, set result to y
cmp rax, rdx ; is max(x,y) less than z?
cmovl rax, rdx ; if so, set result to z
ret ; the max will be in rax
// File name cnams.c
// The c code
#include <stdio.h>
#include <inttypes.h>
int64_t Asm(int64_t, int64_t, int64_t);
int main() {
printf("%ld\n", Asm(1, -4, -7));
printf("%ld\n", Asm(2, -6, 1));
printf("%ld\n", Asm(2, 3, 1));
printf("%ld\n", Asm(-2, 4, 3));
printf("%ld\n", Asm(2, -6, 5));
printf("%ld\n", Asm(2, 4, 6));
return 0;
}
- How to compile
$ pico Asm.asm
$ pico cnasm.c
$ nasm -felf64 Asm.asm
$ gcc -o linked casm.c Asm.o
$ ./linked

You might also like