Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Wednesday, March 17, 2010

learning iphone programming

I start to write some code for learning iphone programming especially objective-c.

The first example shows here is an example to call ANSI C API (that is the first thing I want to figure out in the object-c environment).



/** @find_date generate date string from current time
*/
- (char*) find_date
{
static char date_str[80];
struct tm * my_tm = NULL;
char *month_str[12] = {
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};
time_t time_val;
time_val = time(NULL);
my_tm = localtime(&time_val);
sprintf(date_str, "%s%02d", month_str[my_tm->tm_mon], my_tm->tm_mday);
return date_str;
}



And here is the call to find_date. (convert to NSString from C-String)


{
char date_str[80];
sprintf(date_str, "modified: %s", [self find_date]);
label2.text = [NSString stringWithCString: date_str];
}



Now another example, with given date as input, open a file and read the data from the file, returns the buffer which contains the file content.


- (char*) init_jk_quote: (char*) date_str
{
FILE* fp;
char* quote_buf;
int length;

fp = fopen(date_str, "r");
if (fp == NULL) return NULL;
fseek(fp, 0, SEEK_END);
length = ftell(fp);

quote_buf = (char*) malloc(length);
if (quote_buf == NULL) {
fclose(fp);
return NULL;
}
fseek(fp, 0, SEEK_SET);

fread(quote_buf, sizeof(char), length, fp);
fclose(fp);
return quote_buf;
}
- (void) free_jk_quote: (char*) quote_buf
{
if (quote_buf)
free(quote_buf);
quote_buf = NULL;
}




And the call to the functions is demonstrated here.


{
char* quote_text;


quote_text = [self init_jk_quote: "/tmp/March17.txt"];
if (quote_text == NULL || strlen(quote_text)== 0) {
label3.text = @"not supported!";
}
else {
NSString* mystring = [NSString stringWithCString: quote_text];
label3.text = [@"my quote: " stringByAppendingString: mystring];
}
[self free_jk_quote:quote_text];
}




I think I need to break my habit of writing in C, and convert it to objective-c, so how to convert the above functions in object-c style? That is mainly something I need to learn.

Wednesday, June 10, 2009

mips - the next example

This example access an array which is a block of buffer.

/* bar initialize input (or 3 to each element)
*/
int bar(int* in, int n)
{
int ret;
__asm__(
".set push \n" // save assembler option
".set noreorder\n" // suppress reordering
"lw $9, %2 \n" // $9 = n (counter end)
"li $8, 0 \n" // $8 = 0
"li $12, 0 \n" // $12 = 0
"move $4, %1\n" // $4 = in
"loop: \n"
"lw $11, ($4) \n" // $11 = *in
"ori $11, $11, 3 \n" // $11 = $11 || 3
"sw $11, ($4) \n" // *in = $11
"addi $4, $4, 4 \n" // in++ (move to next addess)
"addi $8, $8, 1 \n" // move to next sample
"bne $8, $9, loop \n" // loop
"sw $11, %0 \n" // save $12 to ret
".set pop \n" // restore assembler option
: "=m" (ret) // to return
: "r" (in),"m" (n) // the input
);
return ret;
}


Here the use of $8 is not the best, because the move the array element is by $4, soe the $8 is not most necessary, because $4 can be used to check the range for the loop.

Tuesday, June 9, 2009

mips - a first example

In the following sample, it shows how to send the input (val) and the output (ret) to assembly block.


/* foo returns (val | 3)
*/
int foo(int val)
{
int ret;
__asm__(
".set push\n" // save assembler option
".set noreorder\n" // suppress reordering
"ori $2, %1, %2\n"
"sw $2, %0\n" // save $2 to ret
".set pop\n" // pop assembler option
: "=m" (ret) // ret: memory
: "r" (val), "n" (3) // val: read-only register, n: number
);
return ret;
}



The real code by dump is:

08804554 :
8804554: 27bdffd0 addiu sp,sp,-48
8804558: afbe0020 sw s8,32(sp)
880455c: 03a0f021 move s8,sp
8804560: afc40010 sw a0,16(s8)
8804564: 8fc20010 lw v0,16(s8)
8804568: 34420003 ori v0,v0,0x3
880456c: afc20000 sw v0,0(s8)
8804570: 8fc20000 lw v0,0(s8)
8804574: 03c0e821 move sp,s8
8804578: 8fbe0020 lw s8,32(sp)
880457c: 27bd0030 addiu sp,sp,48
8804580: 03e00008 jr ra
8804584: 00000000 nop

Tuesday, March 24, 2009

setup cygwin

Not fun, but have to make cygwin work.
1. Here is the batch file to start cygwin.

@echo off

SET MAKE_MODE=UNIX
SET PATH=D:\disk\cygwin\bin;%PAtH%
SET HOME=D:\disk\markc
SET CYGWIN=tty binmode
D:
chdir %HOME%
bash


2. A no special .bash_profile in $(HOME).

# source the system wide bashrc if it exists
if [ -e /etc/bash.bashrc ] ; then
source /etc/bash.bashrc
fi

# source the users bashrc if it exists
if [ -e "${HOME}/.bashrc" ] ; then
source "${HOME}/.bashrc"
fi



3. Now, my .bashrc file.

export PATH=/usr/bin:/sbin:/usr/local/bin
export PATH=/usr/local/psp/gcc-2.8.2/bin:$PATH
export PATH=/usr/local/psp/devkit/bin:$PATH:.
export PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ '


The problem I have is '\r' cause the problem to start cygwin. To make cygwin happy with unix file format, we need set the unix file format in vimrc. The following code is my vimrc.

set autoindent
set softtabstop=4
set tabstop=4
set shiftwidth=4
set showmatch
set ignorecase
set background=dark
set hlsearch
set incsearch
set number
syntax on
set fileformats=unix,dos
set fileformat=unix