Computer Systems Fundamentals

#include <stdio.h>
#include <stdlib.h>

// TEXT FILES with numbers in them
int main(int argc, char *argv[]){
    
    if(argc < 2){
        fprintf(stderr,"usage....\n");
        exit(1);
    }
    
    FILE *f = fopen(argv[1],"r");
    if(f == NULL){
        perror("");
        exit(1);
    }
    
    int num;
    while(fscanf(f,"%d",&num) != EOF){
         printf("I read in %d\n",num);
    }   

    fclose(f);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
    
    if(argc < 2){
        fprintf(stderr,"usage....\n");
        exit(1);
    }
    
    FILE *f = fopen(argv[1],"w");
    if(f == NULL){
        perror("");
        exit(1);
    }
    
    int num;
    while(scanf("%d",&num) != EOF){
        fprintf(f,"%d\n",num);
    }   

    fclose(f);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

// TEXT FILES with numbers in them
int main(int argc, char *argv[]){
    
    if(argc < 2){
        fprintf(stderr,"usage....\n");
        exit(1);
    }
    
    FILE *f = fopen(argv[1],"r");
    
    if(f == NULL){
        perror("");
        exit(1);
    }
    
    //int byte;
    //while( (byte = fgetc(f)) != EOF){
    //     printf("0x%02x\n", byte);
    //}  
    
    //print out last byte in the file?
    fseek(f,-1, SEEK_END);
    int byte;
    if( (byte = fgetc(f)) != EOF){
        printf("0x%02x\n", byte);
    } else {
        printf("This is the end of file\n");
    }  
    
    fseek(f,3, SEEK_SET);
   
    if( (byte = fgetc(f)) != EOF){
        printf("0x%02x\n", byte);
    } else {
        printf("This is the end of file\n");
    }  
    
    long current_pos = ftell(f);
    printf("%lu\n",current_pos);
    
    
    fclose(f);
    return 0; 
}
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
    unsigned int u = atoi(argv[1]);
    printf("%u %x\n",u,u);
    int count = 0;
    
    unsigned int mask = 0x80000000;
    for(int i = 0; i < 32; i++){
        if(mask &u){
            count++;
        }
        mask = mask >> 1;
    }

    printf("Has %d bits set\n",count);
    return 0;
}
	.data
x: .word 1234

	.text
main:
	lw $t0, x
	li $t1, 0x80000000 #mask
	li $t2, 0  #count
	li $t3, 0  #i
for:
	bge $t3, 32, end_for
	and $t4, $t1, $t0
	beqz $t4, end_if
if:
	addi $t2, $t2, 1
end_if:
	srl  $t1, $t1, 1
	addi $t3, $t3, 1	#i++
	j for
end_for:

	li $v0, 1
	move $a0, $t2
	syscall
	
	
	li $v0, 0
	jr $ra