puts("Sup?");
In assembly:
mov rdi,someString
extern puts
call puts
ret
section .data
someString:
db "Sup?",0 ; <- the zero indicates the end of the string
printf("The integer is %d\n",17);
Printf is "varargs", meaning it takes a variable number of
arguments, so you can print several integers in one call with something
like printf("The integers: %d, %d, and %d...\n",2,7,9);
In assembly, there is one weird annoyance with printf, that it requires a "16-byte aligned stack".
sub rsp,8 ; stack MUST be 16-byte aligned after call pushes return address...
mov rdi,formatString ; "format string": contains %d for each printed integer
mov rsi,17 ; integer to print
extern printf
call printf
add rsp,8 ; put stack back
ret
section .data
formatString:
db "The integer is %d",0xa,0 ; <- 0xa == newline; 0 == end of string
return getchar();In assembly:
extern getchar
call getchar
; returned character is in rax
ret
; Read the stringThere's another slightly more complex function called fgets that takes a byte count, so it's actually safe even for long or malicious input values:
mov rdi,myString
extern gets
call gets
; Print the string
mov rdi,myString
extern puts
call puts
ret
section .data
myString:
times 1024 db 0 ; big character buffer, 1024 bytes all zeros
; Read the string with "fgets(string,length,stdin)"
mov rdi,myString
mov rsi,1024
extern stdin
mov rdx,QWORD[stdin] ; subtle: push stdin's value, not the pointer!
extern fgets
call fgets
; Print the string
mov rdi,myString
extern puts
call puts
ret
section .data
myString:
times 1024 db 0 ; big character buffer, 1024 bytes all zeros
extern randIf you want a random number from 0 to 10, you need to wrap the big random number around so it's in the range you want. A standard way to do this is to take the random number mod 10, like "rand()%10", or using the horrible idiv assembly interface:
call rand
ret
extern randTo generate a random number from zero up to a power of two, you can just use bitwise AND, like:
call rand
mov edx,0 ; high bits of numerator
mov ecx,10 ; denominator: random number ranges from zero to this
idiv ecx ; divide eax:edx by ecx. rax gets ratio, rdx gets remainder.
mov rax,rdx ; rdx gets remainder after idiv
ret
extern randTo get a different random number stream, call "srand" with an integer. Oddly, it looks like "srand(0)" and "srand(1)" give the same default random number stream.
call rand
and rax,15 ; random number from 0 to 15
ret
mov rdi,2 ; stream to get (different numbers give different rand results)To get a different random number stream each time you run the program, you can get the time as a number of seconds from "time(0)", and then pass that value to srand:
extern srand
call srand
extern rand
call rand
and rax,15 ; random number from 0 to 15
ret
mov rdi,0 ; time(0)
extern time
call time
mov rdi,rax ; number of seconds since 1970
extern srand
call srand
extern rand
call rand
and rax,15 ; random number from 0 to 15
ret